Symmetrical structural pattern matching

ABSTRACT

The invention provides a pattern matching technique based on the symmetrical construction of the code structures representing the matching expression and the filter to be matched. The method of the invention is for determining whether a computer-storable expression matches a filter. A first code structure representing the expression is evaluated for determining a value of the expression. A second code structure representing the filter is analyzed for determining the characteristics of the filter. Then, the value is filtered according to the filter characteristics. The first code structure is constructed from a plurality of first programming language code structure elements and the second code structure is constructed from a plurality of second programming language code structure elements. Each second structure element corresponds to one of the first structure elements. This symmetrical construction of both code structures enables an arbitrary level of structural complexity. The matching operation may be invoked explicitly.

CROSS-REFERENCE TO RELATED APPLICATION

Cross-reference is made to U.S. patent applications Ser. No. 10/046,191, entitled “Bi-Valuation Of Programming Statements”, which is assigned to the same assignee as the present invention and incorporated herein by reference.

BACKGROUND OF THE INVENTION

1. Field of the Invention

The invention generally relates to pattern matching techniques in programming languages and in particular to a method and system for determining whether a computer-storable expression matches a filter.

2. Description of the Related Art

Presently, several techniques have been developed for string pattern matching. The most fundamental underlying theory, i.e. that of regular languages and recognition automatons, has been widely applied in areas ranging from text processing to DNA sequence searches. Expressions describing string patterns are today still loosely integrated with general purpose scripting languages such as Python or Perl, because of their convenience for solving recurrent string related problems such as pattern extraction or replacement.

With the increasing importance of structured information processing, and especially of complex structural transformations involved in data and document interchanges, the growing trend is to propose specialized query or transformation languages that strongly integrate pattern matching facilities. As long as complex structures are considered, e.g. trees or even graphs, language theorists and designers are again looking for expressive, powerful, simple, clear and precise formalisms in order to capture the most fundamental matching operations. However, the richness of complex data structures induces richer transformation requirements, and general solutions for specifying general pattern matching at the right abstraction level have not yet been proposed. Such general pattern matching technique would however be required for application to a broad class of programming languages.

SUMMARY OF THE INVENTION

Given the problems of the existing technologies, it would therefore be advantageous to provide a method and system that enable the specification of matching operations on various and arbitrary complex data structures while keeping the amount of basic building blocks reasonably small.

It would further be advantageous to provide a pattern matching technique that is based on explicit and restricted assumptions, so that it can be embedded into existing or future, specialized or general purpose programming languages.

Further it would be advantageous to provide a pattern matching method and system having underlying syntax and semantics defined on an unambiguous basis and within an extensible and flexible theoretic framework.

Moreover, it would be advantageous to provide an improved pattern matching technique of great simplicity and high expressive power.

The present invention has been made in consideration of the above situation and provides a method, and article of manufacture therefor, of operating a computer system for determining whether a computer-storable expression matches a filter. A first code structure that represents the expression is evaluated for determining a value of the expression. A second code structure that represents the filter is analyzed for determining the characteristics of the filter. The first code structure is constructed from a plurality of first programming language code structure elements and the second code structure is constructed from a plurality of second programming language code structure elements, where each second structure elements corresponds to one of the first structure elements. The value of the expression is then filtered according to the determined filter characteristics.

The present invention further provides a computer system capable of determining whether a computer-storable expression matches a filter. The computer system comprises processing means for evaluating a first code structure that represents the expression for determining a value of the expression, analyzing a second code structure that represents the filter for determining the characteristics of the filter, and filtering the value according to the filter characteristics. The first code structure is constructed from a plurality of first programming language code structure elements and the second code structure is constructed from a plurality of second programming language code structure elements. Each second structure element corresponds to one of the first structure elements.

BRIEF DESCRIPTION OF THE DRAWINGS

The accompanying drawings are incorporated into and form a part of the specification to illustrate several embodiments of the present invention. These drawings, together with the description, serve to explain the principles of the invention. The drawings are only for the purpose of illustrating alternative examples of how the invention can be made and used and are not to be construed as limiting the invention to only the illustrated and described embodiments. Further features and advantages will become apparent from the following and more particular description on the various embodiments of the invention as illustrated in the accompanying drawings, wherein:

FIG. 1 illustrates a computer system according to the invention;

FIG. 2 is a general flow chart illustrating the pattern-matching technique of the invention;

FIG. 3 is a more particular flow chart illustrating the pattern matching technique of the invention;

FIG. 4 is a flowchart illustrating the evaluation statement process of the invention; and

FIG. 5 illustrates an example of the hierarchy of types in a programming language based on the techniques of the invention.

DETAILED DESCRIPTION

Referring now to the drawings and particularly to FIG. 1, which illustrates a computer system of the present invention, processing means 100 is provided that is connected to an input section 110 and an output section 120. The processing means 100 can be of any type and has access to a program code storage 130 that stores the programming language statements and expressions that are operated on by the invention. The system further comprises in the example shown in FIG. 1, a statement evaluation section 140 and a pattern matching section 150 including instructions that allow the processing means 100 to operate as discussed in more detail below.

General Syntax and Operational Semantics

Before going into the details of the invention, an example of a general abstract syntax for terms of the underlying language is provided, where the notation of e encompasses imperative and declarative statements. Operational semantics is described by using the SOS style described in G. D. Plotkin's article “A structural approach to operational semantics”, Technical Report DAIMI-FN-19, Computer Sciences Dept., Århus university, Denmark, 1981, considering “small step” transition in order to cover an eventual extension to concurrent languages with interleaving semantics.

The first set of definitions is now provided that describe basic constructs that can be considered as universal.

e ::= n | s | true | false constants: numerics, strings, booleans none | unit distinguished values e ★ e basic operations (★ ∈ {+, −, *, /}) if e₁ then e₂ else e₃ boolean choice var x = e.e variable declaration x variables e ::= true | false boolean literals e == e | e != e equality and difference comparisons

The construction for the introduction of local variables

-   -   var x=e in e         is often noted     -   let x=e in e         in functional language communities.

More sophisticated computation structures, e.g. sequencing, loops and assignment, with widely understood semantics are described through the following grammar extension:

e ::= e;e sequence x := e simple assignment to a variable *(e) closure computation

A transition system describes an interpreter of the language through a transition relation which defines the computation of any expression e into a new expression e′, and is noted e→e′. Several computation steps such as e→e′→e″ can be abbreviated by using the notation e

e″. By definition, terminating computations will reduce e to a normal value noted v which cannot be reduced anymore; this particular computation step is noted e→o v, and a derivation chain that reduces to a normal value is noted e

o v. The relation →o is formally described through:

$\frac{e->{e^{\prime}\mspace{14mu} e^{\prime}e^{''}}}{e->{\bullet\mspace{14mu} e^{\prime}}}\mspace{14mu}{or}\mspace{14mu}\frac{e->{e^{\prime}\mspace{14mu}{⫬ \left( {e^{\prime}->e^{''}} \right)}}}{e->{\bullet\mspace{14mu} e^{\prime}}}{{\mspace{14mu}\lbrack{norm}\rbrack}.}$

The formal semantics of basic boolean, string and arithmetic operations (noted ★ above) will not be described here, being considered as widely understood.

The computing environment, as well as the use and notation of variables and references will now be described in more detail. The computing environment, noted S, is a mapping from names (of variables) into reduced values:

-   -   S={x^(i) ^(v)}.         with x_(i) ^(v) being a denotation for the mapping of a unique         label x_(i) into a unique reduced value v_(i). Reference         handling requires also another execution structure, a heap     -   H={r_(i) ^(v)}         using the same notation provided that r denotes references. The         introduction of recursive procedures or functions with non-lazy         evaluation would simply require a stack of mappings in order to         handle local environments. The entire environment H, S is         sometimes abbreviated by Γ.

In another notation, the computing environment is noted Γ as the mapping from variable names into reduced values:

-   -   Γ={x_(o) ^(v), . . . , x_(k) ^(v)}

Further, Γ,x^(v) is an abbreviation of H,S∪{x^(v)}, or Γ∪{x^(v)}, respectively, and similarly Γ, r^(v) is an abbreviation of H∪{r^(v)}, S. The transition relation becomes now Γ├e→Γ′├e′ in the general case, although invariant execution structures can be skipped for clarity when required. This relation means “expression e computed in environment H, S becomes e′ in environment H′, S′”, understood that H′ and S′ can be equal to or different from H and S. The following equations describe the semantics of variable access [var] and declaration [d1,d2]:

ti Γ,x^(v)├x→Γ,x^(v)┘v[var]

$\begin{matrix} {\frac{{\Gamma \vdash e_{1}}->{\Gamma^{\prime} \vdash e_{1}^{\prime}}}{{\Gamma \vdash {{var}\mspace{14mu} x}} = {{{e_{1} \cdot e_{2}}->{\Gamma^{\prime} \vdash {{var}\mspace{14mu} x}}} = {e_{1}^{\prime} \cdot e_{2}}}}{\mspace{14mu}\left\lbrack {d1} \right\rbrack}} \\ {\frac{{\Gamma \vdash e_{1}}->{{\bullet\mspace{11mu}\Gamma} \vdash v}}{{{\Gamma \vdash {{var}\mspace{14mu} x}} = {{e_{1} \cdot e_{2}}->\Gamma}},{x^{v} \vdash e_{2}}}{\mspace{14mu}\left\lbrack {d2} \right\rbrack}} \end{matrix}$

Reference creation and dereferencing involve intermediate distinguished values r (references) on which no other operations are defined, as opposed to pointers and pointer arithmetics.

$\frac{{\Gamma \vdash e}->{\Gamma^{\prime} \vdash e^{\prime}}}{{\Gamma \vdash {@e}}->{\Gamma^{\prime} \vdash {@e^{\prime}}}}\mspace{14mu}\left\lbrack {{ref}\; 1} \right\rbrack$ $\frac{{\Gamma \vdash e}->{\Gamma^{\prime} \vdash e^{\prime}}}{{\Gamma \vdash {!e}}->{\Gamma^{\prime} \vdash {!e^{\prime}}}}{\mspace{14mu}\left\lbrack {{dref}1} \right\rbrack}$ Γ├@v→Γ,r^(v)├r[ref2] Γ,r^(v)├!r→Γ,r^(v)├v[dref2]

In [ref2], it is to be mentioned that a new unique reference r is created in the Heap and that this reference is returned as result of the computation of @v.

In the testing operation, environment modifications are allowed during evaluation of the boolean part. This enables the use of a matching operation as a condition for the test.

$\frac{{\Gamma \vdash e_{1}}->{\Gamma^{\prime} \vdash e_{1}^{\prime}}}{{\Gamma \vdash {{if}\mspace{14mu} e_{1}\mspace{14mu}{then}\mspace{14mu} e_{2}\mspace{14mu}{else}\mspace{14mu} e_{3}}}->{\Gamma^{\prime} \vdash {{if}\mspace{14mu} e_{1}^{\prime}\mspace{14mu}{then}\mspace{14mu} e_{2}\mspace{14mu}{else}\mspace{14mu} e_{3}}}}\mspace{14mu}\lbrack{if}\rbrack$

-   -   if true then e₂ else e₃→e₂ [if1] if false then e₂ else e₃→e₃         [if2]

The semantics of the basic numerical operations +, /, *, − is well known in the art. The + operator is polymorphic, i.e. it applies in various semantics to numerals, strings, sequences, concatenations, multisets, disjunctive unions, i.e. m₁+m₂−m₂=m₁, and dictionaries (non-commutative, right priority).

Pattern Matching

The pattern matching technique, which will now be described in more detail, evaluates a first code structure that represents an expression, analyzes a second code structure that represents the filter to be matched by the expression, and filters the value determined by evaluating the first code structure according to filter characteristics determined by the second code structure. The first code structure, hereafter denoted “data structure” and the second code structure, hereafter denoted “pattern structure”, are constructed symmetrically so that matching operations can be specified on various and arbitrary complex data structures, such as strings, sequences, sets, dictionaries and records, but also on trees, DAGs (Directed Acyclic Graphics) and general graphs. This will be shown in more detail hereafter.

In the following, it is assumed that it is possible to declare local variables which will potentially be assigned to parts of or to the entire filtered structures. Expressions, such as e, e_(i) can be literal constants, variables or basic operations, such as e+e, e*e. References are considered because they allow the sharing of substructures and the modeling of DAGs and graphs.

Pattern matching operations make it possible to check a given expression e for whether the expression contains a structural pattern, and to extract a part of its content, in a single operation. A pattern matching operation is made up of three parts: The first part is an expression e against which the filter is to be checked. This part is called the subject. The second and third parts are the pattern-matching operator # and the filter f. The subject can be a variable, a constant, or a more complex expression that will be evaluated prior to matching.

Thus, a pattern matching operation looks like

-   -   e#f.

These operations return a boolean value. If the operation succeeds, meaning that the expression matches the filter and, optionally, that variables have been assigned to a part of the subject's content, then it returns true. If it fails, meaning that the expression did not match the filter structure or that it did not contain a given value, then false is returned.

By convention, normal values are noted v or n, s if they are numeric or strings, respectively. As shown above, a derivation chain that reduces to a normal value is noted e

o v. The general semantics of the matching operation e#f requires in the following order: the evaluation (step 300 in FIG. 3) of the left operand e (normalized to v), the evaluation (step 310) of the right operand, i.e. the filter f, and the application (step 320) of the filter to the normalized value, which returns either true or false. It will be appreciated that the term “value” does not necessarily relate to a numeric value as the expression may be a non-numeric expression. Moreover, the environment might be modified by the matching operation, whatever result is obtained. Normalized filters are noted as bold letters. The formal semantics of matching operation is twofold. The first stage computes a reduced form first for the subject ([match1] iterated) and then for the filter ([f-match1] iterated):

${\frac{{\Gamma \vdash e}->{\Gamma^{\prime} \vdash e^{\prime}}}{{\Gamma \vdash {e\mspace{11mu}\sharp\mspace{11mu} f}}->{\Gamma^{\prime} \vdash {e^{\prime}\mspace{11mu}\sharp\mspace{11mu} f}}}\mspace{14mu}\left\lbrack {{match}1} \right\rbrack}\mspace{11mu}$ $\frac{{\Gamma \vdash f}->{\Gamma^{\prime} \vdash f^{\prime}}}{{\Gamma \vdash {v\mspace{11mu}\sharp\mspace{11mu} f}}->{\Gamma^{\prime} \vdash {v\mspace{11mu}\sharp\mspace{11mu} f^{\prime}}}}\mspace{14mu}\left\lbrack {f\text{-}{{match}1}} \right\rbrack$

The last stage, i.e. the filter application itself, depends on the filter and data structure considered and will be defined case by case in the following.

Examples of structure constructors are now presented in order to illustrate the symmetry between them and the corresponding filter constructors described below. The structure constructors and filter constructors are indicator elements indicating the respective data type:

{e₁, e₂, . . . } Multiset: a collection type in which elements are not ordered and which can contain multiple instances of the same value [e₁, e₂, . . . ] Sequence: an ordered collection of elements of a common type <e₁, e₂, . . . > Tuple: an ordered collection of elements which has a fixed size. Elements need not be of a common type <name₁ = e₁, name₂ = e₂, . . . Record: an unordered collection of named elements {key₁ = e₁, key₂ = e₂, . . . } Dictionary: an unordered collection of elements that are each accessed by a key. Each key must be unique @ (e) References: a reference to a value (itself computed from an expression e) stored in memory ! De-referencing operator, used to access a referenced value

Further, there is provided a + operator that can take different semantics depending on the data structure, e.g. arithmetic addition, string concatenation, set union, etc.

Filters are expressed using a small number of operators and can describe many different structures. Besides this, filters closely resemble the structures to be matched, making them rather easy to specify. Filters can match sequences, multisets, tuples, records, dictionaries, strings and any combination of these structures. All filters are built using three basic filter operators, in addition to filter constructors which use the same notation as their data constructor counterparts. The three basic filter operators are the test operator, the existence operator and the assignment operator.

The test operator, noted % in the present example, is used in conjunction with a constant, variable or complex expression, which will be evaluated prior to matching. It tests the occurrence of the value given by its operand at some point in the structure. For instance,

-   -   e # %‘circus’         tests whether e is a string equal to ‘circus’ or not.     -   e # [%(2+2), %3]         checks that e is a sequence having one item of value 4 and one         item of value 3 in that order.

The existence operator, noted ?, is defined as ‘match any element’. For instance,

-   -   e # <?,?>         matches any tuple that has exactly two elements, like     -   <‘circus’, 47>.

The assignment operator, noted ?x, where x is a variable name, is used to extract a part of the subject and assign this part to x. For instance,

-   -   e # <%‘circus’,?,?y>         will succeed if e is a tuple containing three elements. The         first element has to be the string ‘circus’, the second one can         be anything, and the value of the third element is assigned to         variable y. The same pattern matching operation on a sequence         would be very similar:     -   e # [%‘circus’,?,?y]         changing only the constructor from tuple to sequence.

In addition to these basic filter operators, there is provided a concatenation operator, noted ++, that combines compatible elements. It can be seen as the dual operator of the concatenation operator (noted +, see above) of data structures. For instance, the string ‘circus’ can be thought of as the concatenation of three substrings

-   -   ‘ci’+‘r’+‘cus’.

In a symmetrical way, the filter appears as

-   -   e # % ‘ci’ ++ %‘r’ ++ %‘cus’.

For finding strings containing character ‘r’, the filter

-   -   ? ++ %‘r’ ++ ?         can be used, which means zero or more elements, plus the letter         ‘r’, and a further zero or more element. This operator can be         used with any structure, making it possible to take other         elements into account. For instance, e # [?x] matches only         sequences consisting of exactly one element, and assigns this         element to x. To express the fact that a sequence e must contain         at least one element, without any ordering, ? combined with the         ++ operator can be used:     -   e # ? ++ [?x] ++ ?.

For illustrating the advantages of the present structural pattern matching scheme, more complex examples are now given for strings, tuples, sequences, records, multisets and dictionaries.

-   -   e # ? ++ %‘r’ ++?x         will match any string containing ‘r’ and assign the substring         beginning after the first occurrence of ‘r’ to variable x; if         e-‘circus’, then x will be assigned ‘cus’; if e=‘red’, then x         will be assigned ‘ed’.     -   e # [<f1=?x>] ++ ?         will match any sequence containing one record as the first         element. This record must contain a field f1; the value of this         field will be assigned to variable x.     -   [<f1=10>,<f2=‘s’>,<f1=3>] # [<f1=?x>]++?y         will succeed; x will be assigned 10 and y will be assigned         [<f2=‘s’>, <f1=3>].     -   e # {? ++ [<?, ?x>]} ++ ?         will match any multiset containing at least one sequence whose         elements are tuples made up of two elements and will assign the         second element of the last tuple of a sequence in this multiset         to variable x. It will select the last tuple of a sequence         because the pattern structure includes ? ++ [<?, ?x>] instead of         [<?, ?x>] ++ ? which would have selected the first tuple.     -   {‘ab’, ‘cd’, ‘ef’} # {%‘ef’,%‘ab’,%‘cd’}         will match since the order is irrelevant in sets.

Pattern matching can further be used to retrieve values from a dictionary. Suppose a dictionary which maps some strings to some sequences,

-   -   dict # {‘k1’=?} ++ ?         will succeed if dict contains a key ‘k1’, and     -   dict # {‘k2’=?x} ++ ?         will succeed if dict contains key ‘k2’ and will assign the         corresponding sequence to variable x.

Since pattern matching operations return boolean values, they can be composed using logical connectors. It is then possible to express filters which succeed only if the subject does not match the filter, or complex filters. There are three composition operators, “not”, “or” and “and”.

“not” will succeed if the subject does not match the filter. For instance,

-   -   e # not (? ++ %‘r’ ++ ?)         will succeed only if string e does not contain the character         ‘r’.

“or” takes two filter arguments, which are matched from left to right. For instance

-   -   e # %‘c’ ++ ? or ? ++ %‘r’ ++ ?         will succeed only if string e begins with the character ‘c’ or         if it contains the character ‘r’.

“and” works in a similar way as “or”. It succeeds if the expression matches both filters. For instance,

-   -   e # <?,?> and ?x         allows checking that e is a two-element tuple and assigns e to         x.

There are also provided “Kleene” operators * and ⁺ that work with the known semantics, “zero or more elements” and “at least one element”, respectively. For instance,

-   -   e # %‘abb’ ++ (%‘c’)* ++ %‘dee’         will match any of the following strings: ‘abbdee’, ‘abbcdee’,         and ‘abbcccdee’. The same filter using the second operator,     -   e # %‘abb’ ++ (%‘c’)⁺ ++ %‘dee’         will match the same set of strings except ‘abbdee’. The Kleene         operators can be used with sequences, sets and dictionaries.

As shown above, the described pattern matching technique enables the symmetrical constructions of both pattern matching structures and data structures, so that they reach the same arbitrary level of structural complexity. Further, the matching operation is invoked explicitly, through a dedicated operator. The general form of the structure to be matched is tested as well as the contained (sub-)structures and the assignment of part(s) of the matching structure to variable(s) of the execution environment.

The technique relates to a number of fields including those of language construction, control abstractions and transformations models, rewriting systems theory, term rewriting systems, transformation languages or systems for compilation and language processing, structured document transformation, tree pattern matching, explicit or automatic document transformation systems, and so on.

Further, the technique may serve for building transformation models which are less abstract and more general, than rewrite systems, which perform implicit pattern matching and apply built-in strategies for rule application. The technique is on the other hand abstract enough to simplify and extend general purpose programming languages. Thus, the technique may play an important role in the design of new transformation techniques or the extension of existing ones.

Four basic topics are addressed in a single approach different to and independent from existing ones that are currently and conceptually less efficient: the definition of a data model, the definition of matching operations, the definition of a transformation model, and the integration of the transformation model in a programming language.

The definition of a data model is to give a precise form to the notion of data structures, which need to be rich and clear for the programmer but at the same time kept as simple as possible. The computational properties of each kind of structure should be stressed to reach completeness at minimal cost. For instance, sequences are ordered structures that are useful for handling stacks, fifo's, or to memorize intermediate nodes when walking through a tree, whereas multisets are useful for managing “bags” of unorganized data.

The technique described above proposes a “universal” data model based on a synthesis of the most commonly used data types. These are chosen in order to avoid redundancy and to enable the construction of heterogeneous structures of arbitrary complexity while being kept simple and explicit. However, applying the technique does not require using exactly the proposed type set, but only applying the symmetrical construction principle, both to data structures and to pattern structures.

The definition of matching operations is clearly related to the previous point. This covers three fundamental issues: identifying and testing the “form” of the structure, (step 200 in FIG. 2); check the value of the structure (or a part of it) (step 210); and extracting a part of the information stored in the structure (step 220).

The technique allows for building pattern matching structures in such a way that the required form is made explicit, thus clear to the programmer who specifies it. Parts of the subject structure which are not of interest are abstracted through a basic existence filter noted “?”. Values inside the structure can be checked as equal to arbitrary complex values, thanks to a basic testing filter noted “%”, e being a potentially complex expression. Finally, information located in the subject structure can be extracted and copied to any variable that is available in the execution environment, by using a basic assignment filter, noted “?x”, where x is a variable name.

The definition of a transformation model starts from the finding that matching operations are just intermediate steps of realistic structure transformation processes and should therefore be put in a more general context: How are transformation steps described, chained and ordered? How are structures scanned and what is the result of the overall transformation? How is the contextual information extracted that is used during matching operations: is it simply transferred into the output structure(s) and/or does it drive the transformation itself?

The explicit invocation of the matching operation, according to the present technique, allows one to see it as a boolean evaluation. The transformation model can therefore take an useful form, depending on the requirements. The transformations can cascade matching operations by using rules like e₁ # f₁

e₂ # f₂

. . . , or composed sequentially or through complex networks of “if-then-else” statements, possibly comprising computation on the extracted context. Output structures, the result of the transformation, are constructed in a coherent way by using the available data models and contextual information, possibly transformed by other computational means.

Depending on the definition of the transformation model and the expected computational power of the transformation, one can consider either to extend the pattern matching with language constructs to specify internal computation, as a complement to matching operations, or to integrate the transformation model itself into a more general programming language, as an extension which increases expressiveness. Both options are made possible by this technique, thanks to the explicit matching operator and the few general, and modular, assumptions regarding the underlying language.

Considering the above observations, it will be appreciated that the described pattern matching technique is in particular suited for supporting general typing mechanisms. Type checking improves the global reliability of programming languages, and provides efficient means for runtime optimization.

Pattern Matching Syntax and Operational Semantics

As noted above, the general abstract syntax uses e for expressions and f for pattern matching filters. Matching operations are invoked through the operator e # f where the left expression e is the subject and the right operant f is the filter or pattern.

There are provided four basic operators plus combinators (or filter connectors):

f ::= ? existence test ?x existence test and assignment to variable x %e equality check @ f ref. filter f ::= f ++ f filter composition f* | f* kleene-like operators f and f | f or f | not f boolean (ordered) connectives

The first two filters ? and ?x are under normal form. The computation of the equality check and of reference filters requires the computation of their embedded expression:

$\frac{e->e^{\prime}}{{\%\; e}->{\%\; e^{\prime}}}{\;\;}\left\lbrack {f\text{-}{anti}} \right\rbrack$ $\frac{f->f^{\prime}}{{@f}->{@f^{\prime}}}{\;\;}\left\lbrack {f{\text{-}@}} \right\rbrack$

By using a meta symbol ★ ∈ {++, and, or}, we define a family of equations valid for all binary combinators:

$\frac{f_{1}->f_{1}^{\prime}}{{f_{1}\;\bigstar\; f_{2}}->{f_{1}^{\prime}\;\bigstar\; f_{2}}}\left\lbrack {f\text{-}{left}} \right\rbrack$ $\frac{f_{2}->f_{2}^{\prime}}{{f_{1}\;\bigstar\; f_{2}}->{f_{1}\;\bigstar\; f_{2}^{\prime}}}\left\lbrack {f\text{-}{right}} \right\rbrack$

The reduction of the kleene-like operator f* preserves the structure of the filter ([f*]) whereas the computation of f⁺ is a pure rewriting ([f⁺]):

$\frac{f->f^{\prime}}{f^{*}->f^{\prime^{*}}}\left\lbrack f^{*} \right\rbrack$ f⁺→f++f*[f⁺]

In the semantics of filter application (matching), v and w denote reduced terms:

ti v#?→true[m-free2] Γ,x^(v)├w#?x→Γ,x^(w)├true[m-free1]

${\frac{\Gamma,{{r^{v} \vdash {v\mspace{11mu}\sharp\mspace{11mu} f}}->\Gamma^{\prime}},{r^{v} \vdash {true}}}{\Gamma,{{r^{v} \vdash {r\mspace{11mu}{\sharp\;@f}}}->\Gamma^{\prime}},{r^{v} \vdash {true}}}\left\lbrack {m{\text{-}@}} \right\rbrack}\mspace{45mu}{\frac{v_{1} = {v_{2}->{{true}\text{/}{false}}}}{{v_{1}\;\sharp\mspace{11mu}\% v_{2}}->{{true}\text{/}{false}}}\left\lbrack {m\text{-}\%} \right\rbrack}$

It is to be mentioned that [m-%] uses again the structural equality on structures. This last is defined for all considered structures as s₁=s₂ iff s₁ ⊂s₂ and s₁ ⊃s₂. The inclusion itself is straight forward for strings, sequences and multisets; for dictionaries, it corresponds to the inclusion of key/value pairs.

The composition combinator ++ applies only to strings, sequences, multisets or dictionaries. It can be abstracted over these various data structures, noted s, by using the + operator which models concatenation for strings and sequences, disjoint union for multisets and non-commutative union for records and dictionaries. As keys must be unique in a dictionary, the key/value pairs of the right hand operand override the key/value pairs of the left hand operand, if required. This does not impact the semantics of the related matching operation. The following definition uses again the equality relation on structures:

$\begin{matrix} \begin{matrix} \left\lbrack {m\text{-}{fadd}\; 1} \right\rbrack \\ \frac{{\exists s_{1}},{\left. s_{2} \middle| {s_{1} + s_{2}} \right. = {{s\mspace{25mu} s_{1}\;\sharp\mspace{11mu} f_{1}}->{{{true}\mspace{25mu} s_{2}\sharp\mspace{11mu} f_{2}}->{true}}}}}{{s\mspace{11mu}\sharp\mspace{11mu}{f_{1}++}f_{2}}->{true}} \end{matrix} \\ {{\forall s_{1}},{{{s_{2}\mspace{14mu}{such}\mspace{14mu}{that}\mspace{14mu} s_{1}} + s_{2}} = {s\left\{ \begin{matrix} {\frac{{s_{1}\;\sharp\mspace{11mu} f_{1}}->{false}}{{s\mspace{11mu}\sharp\mspace{11mu}{f_{1}++}f_{2}}->{false}}\left\lbrack {m\text{-}{fadd}\; 2} \right\rbrack} \\ {\frac{{s_{1}\;\sharp\mspace{11mu} f_{1}}->{{{true}{\mspace{20mu}\;}s_{2}\sharp\mspace{11mu} f_{2}}->{false}}}{{s\mspace{11mu}\sharp\mspace{11mu}{f_{1}++}f_{2}}->{false}}\left\lbrack {m\text{-}{fadd}\; 3} \right\rbrack} \end{matrix} \right.}}} \end{matrix}$

Similarly, in the following definition which applies only to strings, sequences, sets and dictionaries, the term ε is an abstraction over empty string, empty sequence, empty set and empty dictionary:

$\begin{matrix} \left\lbrack m^{*} \right\rbrack \\ {{{if}\mspace{14mu} s} \neq {ɛ\mspace{34mu}\frac{{\exists s_{1}},{\left. s_{2} \middle| s \right. = {{s_{1} + {s_{2}\mspace{25mu} s_{1}\;\sharp\mspace{11mu} f}}->{{{true}\mspace{31mu} s_{2}\sharp\mspace{11mu} f^{*}}->{true}}}}}{{s\mspace{11mu}\sharp\mspace{11mu} f^{*}}->{true}}}} \end{matrix}$ ε#f*→true[m*b]

In the following equation, the order of operands is important, and computing environments can be altered by unsuccessful operations. This enables more efficient implementations since no specific processing is required in order to preserve the original environment:

$\begin{matrix} \left\lbrack {m\text{-}{and}\; 1} \right\rbrack \\ \frac{{\Gamma \vdash {v\mspace{11mu}\sharp\mspace{11mu} f_{1}}}->{{\Gamma^{\prime} \vdash {{true}\mspace{31mu}\Gamma^{\prime}} \vdash {v\mspace{11mu}\sharp\mspace{11mu} f_{2}}}->{\Gamma^{''} \vdash {true}}}}{{\Gamma \vdash {v\mspace{11mu}\sharp\mspace{11mu} f_{1}\mspace{14mu}{and}\mspace{14mu} f_{2}}}->{\Gamma^{''} \vdash {true}}} \end{matrix}$ $\begin{matrix} \left\lbrack {m\text{-}{and}\; 2} \right\rbrack \\ \frac{{\Gamma \vdash {v\mspace{11mu}\sharp\mspace{11mu} f_{1}}}->{\Gamma^{\prime} \vdash {false}}}{{\Gamma \vdash {v\mspace{11mu}\sharp\mspace{11mu} f_{1}\mspace{14mu}{and}\mspace{14mu} f_{2}}}->{\Gamma^{\prime} \vdash {false}}} \end{matrix}$ $\begin{matrix} \left\lbrack {m\text{-}{and}\; 3} \right\rbrack \\ \frac{{\Gamma \vdash {v\mspace{11mu}\sharp\mspace{11mu} f_{1}}}->{{\Gamma^{\prime} \vdash {{true}\mspace{31mu}\Gamma^{\prime}} \vdash {v\mspace{11mu}\sharp\mspace{11mu} f_{2}}}->{\Gamma^{''} \vdash {false}}}}{{\Gamma \vdash {v\mspace{11mu}\sharp\mspace{11mu} f_{1}\mspace{14mu}{and}\mspace{14mu} f_{2}}}->{\Gamma^{''} \vdash {false}}} \end{matrix}$

A similar behavior is defined for “or” filters:

$\begin{matrix} \left\lbrack {m\text{-}{or}\; 1} \right\rbrack \\ \frac{{\Gamma \vdash {v\mspace{11mu}\sharp\mspace{11mu} f_{1}}}->{\Gamma^{\prime} \vdash {true}}}{{\Gamma \vdash {v\mspace{11mu}\sharp\mspace{11mu} f_{1}\mspace{14mu}{or}\mspace{14mu} f_{2}}}->{\Gamma^{\prime} \vdash {true}}} \end{matrix}$ $\begin{matrix} \left\lbrack {m\text{-}{or}\; 2} \right\rbrack \\ \frac{{\Gamma \vdash {v\mspace{11mu}\sharp\mspace{11mu} f_{1}}}->{{\Gamma^{\prime} \vdash {{false}\mspace{31mu}\Gamma^{\prime}} \vdash {v\mspace{11mu}\sharp\mspace{11mu} f_{2}}}->{\Gamma^{''} \vdash {true}}}}{{\Gamma \vdash {v\mspace{11mu}\sharp\mspace{11mu} f_{1}\mspace{14mu}{or}\mspace{14mu} f_{2}}}->{\Gamma^{''} \vdash {true}}} \end{matrix}$ $\begin{matrix} \left\lbrack {m\text{-}{or}\; 3} \right\rbrack \\ \frac{{\Gamma \vdash {v\mspace{11mu}\sharp\mspace{11mu} f_{1}}}->{{\Gamma^{\prime} \vdash {{false}\mspace{31mu}\Gamma^{\prime}} \vdash {v\mspace{11mu}\sharp\mspace{11mu} f_{2}}}->{\Gamma^{''} \vdash {false}}}}{{\Gamma \vdash {v\mspace{11mu}\sharp\mspace{11mu} f_{1}\mspace{14mu}{or}\mspace{14mu} f_{2}}}->{\Gamma^{''} \vdash {false}}} \end{matrix}$

Similarly, the “not” filter is:

$\frac{{\Gamma \vdash {v\mspace{11mu}\sharp\mspace{11mu} f_{1}}}->{\Gamma^{\prime} \vdash {{true}\text{/}{false}}}}{{\Gamma \vdash {v\mspace{11mu}\sharp\mspace{14mu}{not}\mspace{14mu} f_{1}}}->{\Gamma^{\prime} \vdash {{false}\text{/}{true}}}}\left\lbrack {m\text{-}{not}} \right\rbrack$

Of course, backtracking might be convenient in cases where the matching failed and still let the environment changed. Further, a solution by the mean of an explicit restoring operation v(f), may have semantics

$\frac{f->f^{\prime}}{{V(f)}->{V\left( f^{\prime} \right)}}\left\lbrack {f\text{-}V} \right\rbrack$ $\frac{{\Gamma \vdash {v\mspace{11mu}\sharp\mspace{11mu} f}}->{\Gamma^{\prime} \vdash {true}}}{\left. {\Gamma \vdash {v\mspace{11mu}\sharp\mspace{11mu}{V(f)}}} \right)->{\Gamma^{\prime} \vdash {true}}}\left\lbrack {m\text{-}{Va}} \right\rbrack$ $\frac{{\Gamma \vdash {v\mspace{11mu}\sharp\mspace{11mu} f}}->{\Gamma^{\prime} \vdash {false}}}{{\Gamma \vdash {v\mspace{11mu}\sharp\mspace{11mu}{V(f)}}}->{\Gamma \vdash {false}}}\left\lbrack {m\text{-}{Vb}} \right\rbrack$

The symmetrical construction of data and filter structures will now be described in more detail in the examples of tuples, sequences, multisets, dictionaries and records.

Tuples allow to handle heterogeneous structures of fixed length. The present description will present only constructors. Other common operations on tuple structures are: access to items through indexing notations such as t[i], in order to fetch the i^(th) item, or assignment t[i]:=e, in order to set the i^(th) item, provided that i corresponds to the actual size of the tuple. Tuples are particularly useful for computing Cartesian products such as done in relational algebra and related languages:

e ::= <e₀, . . . , e_(k)> tuple construction (k + 1 items) f ::= <f₀, . . . , f_(k)> filter for tuples of k + 1 elements

In the following part, {tilde over (e)} is noted as a shorthand for e₀, . . . , e_(k). The computation of a tuple (or a filter tuple) is defined by the successive computation of all subterms, in the order of occurrence. A tuple or filter tuple is reduced when all subterms or sub-filters are reduced:

$\frac{e_{i}->e_{i}^{\prime}}{\left\langle {v_{0},\ldots\;,\mspace{11mu} e_{i},\ldots\;,e_{k}} \right\rangle->\left\langle {v_{0},\ldots\;,\mspace{11mu} e_{i}^{\prime},\ldots\;,e_{k}} \right\rangle}\lbrack{tuple}\rbrack$ $\frac{f_{i}->f_{i}^{\prime}}{\left\langle {f_{0},\ldots\;,f_{i},\ldots\;,f_{k}} \right\rangle->\left\langle {f_{0},\ldots\;,\mspace{11mu} f_{i}^{\prime},\ldots\;,f_{k}} \right\rangle}\left\lbrack {f\text{-}{tuple}} \right\rbrack$

Matching for tuples is performed by matching all subfilters to corresponding substructures. If the subject v is not a tuple, or if cardinalities are different, the operation returns “false” ([m-tuple]):

$\frac{{\Gamma \vdash {v_{0}\mspace{11mu}\sharp\mspace{11mu} f_{0}}}->{{\Gamma^{(1)} \vdash {{true}\mspace{14mu}\cdots\mspace{14mu}\Gamma^{({k - 1})}} \vdash {v_{k}\mspace{11mu}\sharp\mspace{11mu} f_{k}}}->{\Gamma^{(k)} \vdash {true}}}}{{\Gamma \vdash {\left\langle {v_{0},\ldots\;,v_{k}} \right\rangle\;\sharp\;\left\langle {f_{0},\ldots\;,f_{k}} \right\rangle}}->{\Gamma^{(k)} \vdash {true}}}\left\lbrack {m\text{-}{tuple}} \right\rbrack$

It is to be mentioned that potential modifications of execution context are propagated through the whole matching process.

Sequences, multisets and dictionaries are defined as shown below:

e ::= [e₀, . . . , e_(k)] | {e₀, . . . , e_(k)} (ordered) sequences, multisets ::= {e₀= e₀′, . . . , e_(k)=e_(k)′} dictionaries (unordered key/value pairs) ::= [ ] | { } | {=} empty sequence, multiset, dictionary f ::= [f₀, . . . , f_(k)] | {f₀, . . . , f_(k)} sequence/multiset filters (k ≧ 0) {f₀ = f₀′, . . . , f_(k) = f_(k)′} dictionary filters (k ≧ 0) with:

$\begin{matrix} {\frac{\left. e_{i}\rightarrow e_{i}^{\prime} \right.}{\left. \left\lbrack {v_{0},\;\ldots\mspace{11mu},e_{i},\mspace{11mu}\ldots\mspace{11mu},e_{k}} \right\rbrack\rightarrow\left\lbrack {v_{0},\;\ldots\mspace{11mu},e_{i}^{\prime},\;\ldots\mspace{11mu},e_{k}} \right\rbrack \right.}\mspace{14mu}\lbrack{seq}\rbrack} \\ {\frac{\left. f_{i}\rightarrow f_{i}^{\prime} \right.}{\left. \left\lbrack {f_{0},\;\ldots\mspace{11mu},f_{i},\;\ldots\mspace{11mu},f_{k}} \right\rbrack\rightarrow\left\lbrack {f_{0},\;\ldots\mspace{11mu},f_{i}^{\prime},\;\ldots\mspace{11mu},f_{k}} \right\rbrack \right.}\mspace{11mu}{\text{[}\text{f-seq}\text{]}}} \end{matrix}$

Equivalent equations for sets, called [set], [set2], [f-set] and [f-set2], are strictly similar to the previous one:

$\begin{matrix} {\frac{\left. e_{i}\rightarrow e_{i}^{\prime} \right.}{\left. \left\{ {v_{0},\;\ldots\mspace{11mu},e_{i},\;\ldots\mspace{11mu},e_{k}} \right\}\rightarrow\left\{ {v_{0},\;\ldots\mspace{11mu},e_{i}^{\prime},\;\ldots\mspace{11mu},e_{k}} \right\} \right.}\mspace{14mu}\lbrack{set}\rbrack} \\ {\frac{\left. f_{i}\rightarrow f_{i}^{\prime} \right.}{\left. \left\{ {f_{0},\;\ldots\mspace{11mu},f_{i},\;\ldots\mspace{11mu},f_{k}} \right\}\rightarrow\left\{ {f_{0},\;\ldots\mspace{11mu},f_{i}^{\prime},\;\ldots\mspace{11mu},f_{k}} \right\} \right.}\mspace{14mu}{\text{[}\text{f-set}\text{]}}} \end{matrix}$

When matching sets S^([0,k]) denotes the set {v₀, . . . , v_(k)} and SF^([0,k]) the filter {f₀, . . . , f_(k)}. If i ∈ [0,k], the S^([0,k]−{i}) is equivalent to {v₀, . . . , v_(i−1), v_(i+1), . . . v_(k)}, and similarly for SF^([0,k]−{i}). Using this notation, the matching over sets is recursively defined as:

$\frac{\begin{matrix} {{\exists i},{j \in {\left\lbrack {0,k} \right\rbrack\mspace{14mu}{such}\mspace{14mu}{that}}}} \\ \left\{ \begin{matrix} {\Gamma \vdash} & {v_{i}\mspace{14mu}\sharp\mspace{14mu} f_{j}} & \rightarrow & {\Gamma^{\prime} \vdash {true}} \\ {\Gamma^{\prime} \vdash} & {S^{{\lbrack{0,k}\rbrack} - {\{ i\}}}\mspace{14mu}\sharp\mspace{14mu}{SF}^{{\lbrack{0,k}\rbrack} - {\{ j\}}}} & \rightarrow & {\Gamma^{\prime\prime} \vdash {true}} \end{matrix} \right. \end{matrix}}{\Gamma \vdash \left. {S^{\lbrack{0,k}\rbrack}\mspace{14mu}\sharp\mspace{14mu}{SF}^{\lbrack{0,k}\rbrack}}\rightarrow\Gamma^{\prime\prime} \right. \vdash {true}}\mspace{11mu}{\text{[}\text{m-set}\text{]}}$

This equation does not express any ordering in exploring the search space. It just requires that each value in S matches a corresponding filter in SF. Note that the computing environment can be modified throughout matching operations.

Dictionaries are computed by successive evaluation of key/value pairs, following the occurrence order:

$\begin{matrix} {\frac{\left. e_{i}\rightarrow e_{i}^{\prime\prime} \right.}{\begin{matrix} \left. \left\{ {{v_{\; 0} = v_{\; 0}^{\;\prime}},\;\ldots\mspace{11mu},{e_{\; i} = e_{\; i}^{\;\prime}},\;\ldots\mspace{11mu},{e_{\; k} = e_{\; k}^{\;\prime}}} \right\}\rightarrow \right. \\ \left\{ {{v_{\; 0} = v_{\; 0}^{\;\prime}},\;\ldots\mspace{11mu},{e_{\; i}^{\;{\prime\prime}} = e_{\; i}^{\;\prime}},\;\ldots\mspace{11mu},{e_{\; k} = e_{\; k}^{\;\prime}}} \right\} \end{matrix}}\mspace{14mu}\lbrack{dic}\rbrack} \\ {\frac{\left. e_{i}^{\prime}\rightarrow e_{i}^{\prime\prime} \right.}{\begin{matrix} \left. \left\{ {{v_{\; 0} = v_{\; 0}^{\;\prime}},\;\ldots\mspace{11mu},{v_{\; i} = e_{\; i}^{\;\prime}},\;\ldots\mspace{11mu},{e_{\; k} = e_{\; k}^{\;\prime}}} \right\}\rightarrow \right. \\ \left\{ {{v_{\; 0} = v_{\; 0}^{\;\prime}},\;\ldots\mspace{11mu},{v_{\; i} = e_{\; i}^{\;{\prime\prime}}},\;\ldots\mspace{11mu},{e_{\; k} = e_{\; k}^{\;\prime}}} \right\} \end{matrix}}\mspace{14mu}\left\lbrack {{dic}2} \right\rbrack} \end{matrix}$

Computation of filter dictionaries is strictly similar, and therefore, corresponding equations [f-dic], [f-dic2] will not be shown here.

When matching dictionaries, D^([0,k]) denotes the dictionary {v₀=V₀′, . . . , v_(k)=v_(k)′} and DF^([0,k]) the filter {f₀=f′₀, . . . , f_(k)=f′_(k)}. Using this notation, the matching over dictionaries is recursively defined as:

$\frac{\begin{matrix} {{\exists i},{j \in {\left\lbrack {0,k} \right\rbrack\mspace{14mu}{such}\mspace{14mu}{that}}}} \\ \left\{ \begin{matrix} {{\Gamma\; \vdash}\;} & {\;{v_{\; i}\mspace{14mu}\sharp\mspace{14mu} f_{\; j}}} & \left. \;\rightarrow\; \right. & {\;{\Gamma^{\;\prime}\; \vdash \;{true}}} \\ {\;{\Gamma^{\;\prime}\; \vdash}\;} & {\;{v_{\; i}^{\;\prime}\mspace{14mu}\sharp\mspace{14mu} f_{\; j}^{\;\prime}}} & \left. \;\rightarrow\; \right. & {\;{\Gamma^{\;{\prime\prime}}\; \vdash \;{true}}} \\ {\;{\Gamma^{\;{\prime\prime}}\; \vdash}\;} & {\;{D^{{\lbrack{0,\; k}\rbrack}\; - \;{\{ i\}}}\mspace{14mu}\sharp\mspace{14mu}{DF}^{{\lbrack{0,\; k}\rbrack}\; - \;{\{ j\}}}}} & \left. \;\rightarrow\; \right. & {\;{\Gamma^{\;{\prime\prime\prime}}\; \vdash \;{true}}} \end{matrix} \right. \end{matrix}}{\Gamma \vdash \left. {D^{\lbrack{0,k}\rbrack}\mspace{11mu}\sharp\mspace{11mu}{DF}^{\lbrack{0,k}\rbrack}}\rightarrow\Gamma^{\prime\prime\prime} \right. \vdash {true}}\mspace{14mu}{\text{[}\text{m-dic}\text{]}}$

This equation does not express any ordering in exploring the search space. It just requires that each key/value pair matches a corresponding filter-key/filter-value pair. Note that the computing environment can be modified throughout matching operations, and that keys are matched prior to values.

Records are unordered and heterogeneous collection of values which can be statically designed by a name called “member”. Standard operations on such data structures s are member access, e.g. a:=s.m, or member assignment, e.g. s.m:=10. Record provide useful mnemonics in order to select a part of complex structures and are well known for their expressive richness in data modeling:

e ::= <m₀ = e₀, . . . , m_(k) = e_(k)> records (unordered member-name/ value pairs) (k ≧ 0) f ::= <m₀ = f₀, . . . , m_(k) = f_(k)> record filters (k ≧ 0)

The reduction of data and filters follows the order of occurrence:

$\frac{\left. e_{i}\rightarrow e_{i}^{\prime} \right.}{\left. \left\langle {{m_{0} = v_{0}},\;\ldots\mspace{11mu},{m_{i} = e_{i}},\;\ldots\mspace{11mu},{m_{k} = e_{k}}} \right\rangle\rightarrow\left\langle {{m_{0} = v_{0}},\;\ldots\mspace{11mu},{m_{i} = e_{i}^{\prime}},\;\ldots\mspace{11mu},{m_{k} = e_{k}}} \right\rangle \right.}\mspace{14mu}{\text{[}\text{e-rec}\text{]}}$

Filter records are similarly defined:

$\;{\frac{\left. f_{i}\rightarrow f_{i}^{\prime} \right.}{\quad\begin{matrix} \left. \left\langle {{m_{0} = f_{0}},\;\ldots\mspace{11mu},{m_{i} = f_{i}},\;\ldots\mspace{11mu},{m_{k} = f_{k}}} \right\rangle\rightarrow \right. \\ \left\langle {{m_{0} = f_{0}},\;\ldots\mspace{11mu},{m_{i} = f_{i}^{\prime}},\;\ldots\mspace{11mu},{m_{k} = f_{k}}} \right\rangle \end{matrix}}\mspace{14mu}\left\lbrack {f\text{-}{record}} \right\rbrack}$

Matching is not order sensitive, i.e. the ordering of filters can differ from the ordering of the subject, but filters are applied through their definition order. Member names must be exactly identical:

$\frac{\begin{matrix} {{\exists\mspace{14mu}{{distinct}\mspace{14mu} i_{0}}},\;\ldots\mspace{11mu},{i_{k} \in {\left\lbrack {0\mspace{11mu}\ldots\mspace{11mu} k} \right\rbrack\mspace{14mu}{such}\mspace{14mu}{that}}}} \\ \left\{ \begin{matrix} {{m_{0}^{\prime} = {{m_{i0}\mspace{14mu}{and}\mspace{14mu}\Gamma} \vdash \left. {v_{i0}\mspace{11mu}\sharp\mspace{11mu} f_{0}}\;\rightarrow\Gamma^{(1)} \right. \vdash \;{true}}}\mspace{45mu}} \\ {\vdots\mspace{205mu}} \\ {m_{k}^{\prime} = {{m_{ik}\mspace{14mu}{and}\mspace{14mu}\Gamma^{({k - 1})}} \vdash \left. {v_{ik}\mspace{11mu}\sharp\mspace{11mu} f_{k}}\;\rightarrow\Gamma^{(k)} \right. \vdash \;{true}}} \end{matrix} \right. \end{matrix}}{\begin{matrix} {\Gamma \vdash \left. {\left\langle {{m_{0} = v_{0}},\;\ldots\mspace{11mu},{m_{k} = v_{k}}} \right\rangle\mspace{11mu}\sharp\mspace{11mu}\left\langle {{m_{0}^{\prime} = f_{0}},\ldots\mspace{11mu},\;{m_{k}^{\prime} = f_{k}}} \right\rangle}\rightarrow \right.} \\ {\Gamma^{(k)} \vdash \;{true}} \end{matrix}\;}{\text{[}\text{m-record}\text{]}}$

The matching fails if one of the sub-matching fails:

$\frac{\begin{matrix} {{\exists\;{{distinct}\mspace{14mu} i_{0}}},\;\ldots\mspace{11mu},{i_{j} \in \left\lbrack {0\mspace{11mu}\ldots\mspace{11mu} k} \right\rbrack},{j \leq {k\mspace{14mu}{such}\mspace{14mu}{that}}}} \\ \left\{ \begin{matrix} {{m_{0}^{\prime} = {{m_{i_{0}}\mspace{14mu}{and}\mspace{14mu}\Gamma} \vdash \left. {v_{i_{0}}\mspace{11mu}\sharp\mspace{11mu} f_{0}}\;\rightarrow\Gamma^{(1)} \right. \vdash \;{true}}}\mspace{50mu}} \\ {\vdots\mspace{191mu}} \\ {m_{j}^{\prime} = {{m_{ij}\mspace{14mu}{and}\mspace{14mu}\Gamma^{({j - 1})}} \vdash \left. {v_{ij}\mspace{11mu}\sharp\mspace{11mu} f_{j}}\;\rightarrow\Gamma^{(j)} \right. \vdash \;{false}}} \end{matrix} \right. \end{matrix}}{\begin{matrix} {\Gamma \vdash \left. {\left\langle {{m_{0} = v_{0}},\;\ldots\mspace{11mu},{m_{k} = v_{k}}} \right\rangle\mspace{11mu}\sharp\mspace{11mu}\left\langle {{m_{0}^{\prime} = f_{0}},\ldots\mspace{11mu},{m_{k}^{\prime} = f_{k}}} \right\rangle}\rightarrow \right.} \\ {{\Gamma^{(j)} \vdash \;{false}}\;} \end{matrix}}\mspace{14mu}{\text{[}\text{m-record}\text{2]}}$ Recursive Filters

Recursive filters and the “do” operator will now be described in more detail.

Recursive filters enable the filtering of trees. For instance,

-   -   e # rec F= <%‘plus’,F,F> or <%‘minus’,F,F> or <?>         succeeds if e is a tree with nodes that are labeled by strings         ‘plus’ and ‘minus’ and with leaves that are tuples containing a         unique element (of any structure).

The “do” operator takes a filter as the left argument and an instruction (or sequence of instructions) as the right argument. Instructions are executed only if the filter is successful.

For instance,

-   -   e # <%12,?> do i:=i+1         increments i only if e is a tuple made of two elements, the         first one being 12. This operator can be particularly useful in         recursive filters.

Using “do” it is possible, for instance, to extract all the numbers from a tree based on the previous example and append them to a list n. The extend filter A is

-   -   rec F= <%‘plus’,F,F> or <%‘minus’,F,F>or (<?x> do n:=n+[x])

In a basic example such as

-   -   <‘plus’,<‘minus’,<1>,<2>>,<4>> # A         where the tree encodes (1−2)+4, the pattern matching operation         succeeds and n is equal to n+[1,2,4] after application.

The syntax and operational semantics of recursive filters and the “do” operator is described hereafter:

f ::= rec F = f | F recursive filter and recursion variable ::= f do e “do” operator (e is executed if f matches)

As described above, these filters are reduced by preserving their structures. Reduction of the “f do e” filter differs from reduction of the “%e” filter because the embedded expression e is not computed. It will be evaluated in the context of the matching operation, and only if required:

$\begin{matrix} {\;{\frac{\left. f\rightarrow f^{\prime} \right.}{{{rec}\mspace{14mu} F} = {\left. f\rightarrow{{rec}\mspace{14mu} F} \right. = f^{\prime}}}\mspace{14mu}{\text{[}\text{e-rec}\text{]}}}} \\ {\frac{\left. f\rightarrow f^{\prime} \right.}{\left. {f\mspace{14mu}{do}\mspace{14mu} e}\rightarrow{f^{\prime}\mspace{14mu}{do}\mspace{14mu} e} \right.}\mspace{14mu}{\text{[}\text{e-do}\text{]}}} \end{matrix}$

Recursive matching is defined by using a substitution operation, as e.g. in the standard β-reduction of the lambda calculus:

$\begin{matrix} {\frac{{\Gamma \vdash {\upsilon\mspace{14mu}\sharp\mspace{14mu}{f\left\lbrack {{{rec}\mspace{14mu} F} = {f/F}} \right\rbrack}}}->{\Gamma^{\prime} \vdash {{true}\text{/}{false}}}}{{\Gamma \vdash {\upsilon\mspace{14mu}\sharp\mspace{14mu}{rec}\mspace{11mu} F}} = {f->{\Gamma^{\prime} \vdash {{true}\text{/}{false}}}}}\mspace{14mu}{\text{[}\text{m-rec}\text{]}}} & \mspace{14mu} \end{matrix}$

In the following [m-do], the evaluation of e is done only if the filtering operation is successful. In that case, the new context is used, thus allowing side-effects such as the memorization of useful intermediate information:

$\begin{matrix} {\frac{{\Gamma \vdash {\upsilon\mspace{14mu}\sharp\mspace{14mu} f}}->{\Gamma^{\prime} \vdash {{true}\mspace{14mu}\Gamma^{\prime}} \vdash {e\bullet\;\Gamma^{''}} \vdash w}}{{\Gamma \vdash {\upsilon\mspace{14mu}\sharp\mspace{14mu} f\mspace{14mu}{do}\mspace{14mu} e}}->{\Gamma^{''} \vdash {true}}}\mspace{14mu}{\text{[}\text{m-do}\text{]}}} \\ {\frac{{\Gamma \vdash {\upsilon\mspace{14mu}\sharp\mspace{14mu} f}}->{\Gamma^{\prime} \vdash {false}}}{{\Gamma \vdash {\upsilon\mspace{14mu}\sharp\mspace{14mu} f\mspace{14mu}{do}\mspace{14mu} e}}->{\Gamma^{\prime} \vdash {false}}}\mspace{14mu}{\text{[}\text{m-do}\text{2]}}} \end{matrix}$

For illustration purposes, the technique described above is now applied to an example of managing a database of book references. The data is stored in a Set structure (unordered collection of records) describe hereafter.

const DBase = { < year = ‘1994’ , title = ‘TCP/IP lllustrated’ , author = [ < last = ‘Stevens’ , first = ‘W.’ > ] , publisher = ‘Addison-Wesley’ , price = 65.95 >, < title = ‘Advanced Programming in the Unix environment’ , year = ‘1992’ , author = [ < last = ‘Stevens’ , first = ‘W.’ > ] , publisher = ‘Addison-Wesley’ , price = 65.95 >, < year = ‘2000’ , title = ‘Data on the Web’ , author = [ < last = ‘Abiteboul’ , first = ‘Serge’ >, < last = ‘Buneman’ , first = ‘Peter’ >, < last = ‘Suciu’ , first = ‘Dan’ > ] , publisher = ‘Morgan Kaufmann Publishers’ , price = 39.95 >, < year = ‘1999’ , title = ‘The Economics of Technology and Content for Digital TV’ , editor = < last = ‘Gerbarg’ , first = ‘Darcy’ , affiliation = ‘CITI’ >, publisher = ‘Kluwer Academic Publishers’ , price = 129.95 > }

Now, examples are given to extract, filter and recombine book information by using the filtering primitives and structured pattern constructors.

For finding all books from Addison-Wesley that are published between 1990 and 1999, and for storing the titles, a recursive function F1 is defined that also returns the result in a Set:

function F1 (x) is if x#{ < title=?t,publisher=%‘Addison- Wesley’,year=%‘199’++?> ++ ? } ++ ?y then return F1 (y) + { < title = t > } else return {}

If F1 is called with the book database as parameter (written F1(DBase)) the result is

{ < title = ‘Advanced Programming in the Unix environment’ >, < title = ‘TCP/IP Illustrated’ >}

The rest of the information is not return in the result, e.g. the authors. The following variation, which is also perhaps more legible, does this job:

function F1 (x) is if (x#{?book} ++ ?y) and (book#< title=?t,publisher=%‘Addison- Wesley’,year=%‘199’++? > ++ ?) then return F1 (y) + {book} else return { }

This last example shows the interest of having explicit filtering operations: the first application is done on the parameter x and the second on “book”. The equivalent solution below shows the interest of the “and” filter; the previous “and” is just a standard boolean connective.

function F1 (x) is if x#{ (< title=?t,publisher=%‘Addison-Wesley’,year=%‘199’++? > ++ ?) and ?book }++?y then return F1 (y) + {book} else return { }

For finding all publishers known in the database, the filters are used in another construction: the “for” loop, has for instance the following syntax

-   -   for f in e1 do e2         where f is a filter, e₁ a computable expression that evaluates         to a Set or a Sequence, and e₂ any programming statement. This         loop applies the filter to all elements of the Set/Sequence, and         if successful, executes e₂. The solution is then:     -   for <publisher=?p>++? in DBase do (R := R+{p})

This result of this program is found in R, with R being supposed to be empty before the execution:

$\quad\begin{matrix} {R = {\left\{ \quad \right.{‘{{Addison}\text{-}{Wesly}}’},}} \\ {{‘{{Addison}\text{-}{Wesley}}’},} \\ {{‘{{Morgan}\mspace{14mu}{Kaufmann}\mspace{14mu}{Publishers}}’},} \\ \left. {‘{{Kluwer}\mspace{14mu}{Academic}\mspace{14mu}{Publishers}}’} \right\} \end{matrix}$

As can be seen, the Addison-Wesley publisher is repeated twice. One might want to have a real “projection” of the information, and for that, can use an additional filter:

for <publisher=?p>++? in DBase do ( if not (R#{%p}++?) then R:=R+{p} )

The result of this program is as expected:

$\quad\begin{matrix} {R = {\left\{ \quad \right.{‘{{Addison}\text{-}{Wesly}}’},}} \\ {{‘{{Morgan}\mspace{14mu}{Kaufmann}\mspace{14mu}{Publishers}}’},} \\ \left. {‘{{Kluwer}\mspace{14mu}{Academic}\mspace{14mu}{Publishers}}’} \right\} \end{matrix}$

For finding all authors having last names that begin with ‘A’, the following construction can be used:

for <author=?s>++? in DBase do ( for <last=(%‘A’++? and ?auth)>++? in s do ( if not (R#{%auth}++?) then R := R+{auth} ) ) The result will be {‘Abiteboul’}.

It will be appreciated that the presented pattern matching technique is suitable for either designing programming languages specialized in data structure transformation, or for facilitating extensions to existing languages in order to handle such transformations. The construction of data structures and pattern matching structures, i.e. filters, of arbitrary complexity uses symmetrical constructors, thereby allowing for great simplicity. The definition of basic filters, structured filters, recursive filters and logical combinators provides any programmer with means that have a high expressive power and a good level of simplicity. A boolean operator is provided that allows to apply explicitly the pattern matching structure to the subject data structure. Further, a formal operational semantics is provided that defines precisely the nature of filters and of the matching operation as well the relationship with any associated computational model. Moreover, this formalization offers a flexible theoretical framework that can help further integration.

Bi-Valuation of Programming Statements

By means of the statement evaluation section 140, the computer system can perform the process which is depicted in FIG. 4. A programming language statement may include a first and a second sub-statement. In step 400, the first sub-statement is evaluated, and depending on the decision in step 410, the second sub-statement may likewise be evaluated. As will be shown in more detail below, statements exist that require the evaluation of the second sub-statement whereas on the other hand, there are also statements where evaluation of the second sub-statement is not necessary in each case.

When the first and/or the second sub-statement is evaluated in steps 400, 420, an evaluation success result is determined if evaluation succeeds or a distinguished value is returned if evaluation fails. In the following discussion, the distinguished value is noted “none”.

The provision of a distinguished value in addition to the set of possible evaluation success results is called bi-valuation.

The bi-valuation technique makes it possible to combine imperative and declarative statements in one programming language. Basically, both imperative and declarative statements are evaluated when executed and either return “unit”, a value, or “none”. The value “unit” is returned for imperative statements since imperative statements always succeed. A value is returned for declarative statements which succeed, and for declarative statements which fail return “none”.

It is therefore possible to combine both kinds of statements using so-called imperative connectors, like “Then” or “Else” having semantics that are based on the evaluation of the statements. Conditional, imperative, declarative statements and imperative connectors are described below in more detail. Further, it is demonstrated how imperative connectors are used for mixing statements.

In the following, s and s_(i) denote one or more imperative and/or declarative statements combined together by imperative connectors. The term ‘to evaluate s’ means ‘to execute the statements in s and return the evaluation of the last statement in s’. Expressions like e, e_(i) can be literal constants, variables or basic operations like e+e, e*e.

Conditional statements, or if-then-else statements, are noted

-   -   if (e) then (s₁) else (s₂)         where e is a boolean expression. Depending on whether e         evaluates to true or false, s₁ or s₂ is evaluated, respectively.

As opposed to declarative statements, imperative statements always succeed, and evaluate to “unit”. One example is the assignment, noted x:=e, where x is a variable identifier and e is an expression. It assigns the result of the evaluation of e to x. Another example is the closure loop, noted *(s). The loop ends when s evaluates to “none”. Its semantics is as follows:

-   -   if (s != none) then *(s) else unit

Declarative statements are based on conditions. The evaluation of a declarative statement returns a value if the condition(s) is verified, “none” otherwise. Examples are rules, ordered action systems and unordered action systems.

The Rule is noted e→s where condition e can be a boolean or a pattern-matching expression. If the condition is verified, meaning that the pattern has been matched or that the boolean expression has evaluated to true, then the rule fires, triggering the evaluation of the right hand side s. Rules are commonly used in rewriting systems by combining them in constructs similar to action systems.

The (ordered) action system is noted [|s₁, s₂, . . . , s_(n)|] and can be compared with a powerful Switch construct. It is used to combine rules. The action system evaluates them one by one until it finds one that does not evaluate to “none”, i.e. a rule which fires and that has right hand side statements that do not evaluate to “none”. The action system itself returns the result of the evaluation of these statements. Action systems can also contain an imperative statement in the last position which will act as a default case since imperative statements always return “unit”.

The unordered action system is noted {|s₁. s₂, . . . , s_(n)|} and does not guarantee the order of evaluation of rules, i.e. the order of declaration has not semantic meaning.

There are three imperative connectors which make it possible to mix imperative and declarative statements. An example set of imperative connectors is given below:

The sequencing operation “;” separates instructions that are to be executed in sequence.

-   -   s₁; s₂; . . . ; s_(n)         will execute every statement and return the evaluation of s_(n).

The concurrent operators ∥^ and ∥^(v) separate instructions that are to be executed concurrently. They have respectively logical-And-like and logical-Or-like semantics. Basically, s₁∥^s₂ concurrently evaluates s₁ and s₂ returning “none” when at least one of the s_(i) evaluates to “none”; “unit” is returned when both operands evaluate to “unit”. s₁∥^(v)s₂ behaves in the following way: it concurrently evaluates s₁ and s₂ returning “unit” when at least one of the s_(i) evaluates to “unit”, and returning “none” when both operands evaluate to “none”. A more formal definition of the semantics is provided below.

The Else operator s₁ Else s₂ evaluates s₁. If s₁ evaluates to “none”, then s₂ is evaluated. The semantics is as follows:

-   -   var v_(f)=s₁. if (v_(f) == none) then s₂ else v_(f).         v_(f) is a fresh variable, i.e. doesn't occur in the current         context nor in s₁ or s₂.

The Then operator s₁ Then s₂ evaluates s₁. If s₁ does not evaluate to “none”, then s₂ is evaluated. The semantics is as follows:

if (s₁ ! = none) then (s₂) else (none)

The Or operator s₁ Or s₂ evaluates s₁. If s₁ evaluates to “none”, the evaluation of s₂ is returned. If s₁ evaluates to “unit”, s₂ is still evaluated but “unit” is returned, no matter what s₂ evaluates to. The semantics is as follows:

-   -   if (s₁ == none) then (s₂) else(s₂ ; unit).

The And operator s₁ And s₂ evaluates s₁. If s₁ evaluates to “unit”, the evaluation of s₂ is returned. If s₁ evaluates to “none”, s₂ is still evaluated but “none” is returned. The semantics is as follows:

-   -   if (s₁ ! = none) then (s₂) else (s₂ ; none)

For operators And, Or, ∥^ and ∥^(v), s₁ and s₂ have to evaluate to “unit” or “none”, meaning that they have to be imperative expressions.

Some introductory examples are now provided to get familiar with the notation, and then the expressiveness brought by the technique is demonstrated with more complex examples. Variables i and str1 are supposed to be respectively of type Integer and String.

A simple sequence is made of an assignment statement followed by a “while” loop containing a rule: this loop increments i until it reaches 10.

-   -   i :=0;     -   *((i<10)→i:=i+1)

In an action system containing a rule, a nested action system is made of rules and an expression:

[| (i<5) → (str1 := ‘cir’ ; 50 − 3), [| (i == 100) → ‘abcd’, (i == 200) → ‘efgh’ |], 3.1415 |] If i is less than 5, then str1 is assigned the string ‘cir’ and the action system returns 47. Otherwise, if i is equal to 100 or 200, the action system respectively returns strings ‘abcd’ or ‘efgh’. Otherwise, it returns 3.1415. This last expression acts as a ‘default’ case. If it were not present and if i was not previously assigned a value less than 5 or equal to 100 or 200, the action system would have evaluated to “none”.

It is to be noted that action system components do not necessarily contain a simple expression like 50-3 or ‘abcd’ as the last statement. They can also contain imperative expressions; in that case, the action system returns “unit”.

The next more complex example makes use of the pattern matching features described above. Basically, pattern matching expressions can be used in place of boolean expressions in the left hand side of rules; they enable the recognition of patterns and extraction of parts of a data structure. A pattern matching expression evaluates to true if the subject matches and then the rule fires. Otherwise it evaluates to false and the rule returns “none”.

In the present example, some structural data about an XML file stored in string variable str1 are extracted. To this end, several numeric variables are created which will count:

nb_open_tags number of openings tags nb_close_tags number of closing tags nb_emp-tags number of empty tags nb-prefixed_tags number of prefixed tags (the prefix represents a namespace) nb_xrce_tags number of tags having the name ‘xrce’ nb_other_tags number of tags which do not have a prefix and are not labeled ‘xrce’

The first three tests identify the kind of the encountered tag, i.e. opening, closing, empty, whereas the three others examine the content of that tag. Since the last three tests are independent from the previous ones and relevant only when a new tag is encountered, the tests are split in two action systems combined with a Then connector. The variable tags is a string in which each tag name is appended. When all tags have been tested, the results are displayed.

*( [| str1 # ? ++ %‘ <’ ++ ?str2 ++ %‘ >’ ++ ?str1 → nb_open_tags := nb_open_tags+1, str1 # ? ++ %‘ </’ ++ ?str2 ++ %‘ >’ ++ ?str1 → nb_close_tags := nb_close_tags+1, str1 # ? ++ %‘ <’ ++ ?str2 ++ %‘/>’ ++ ?str1 → nb_emp_tags := nb_emp_tags+1 |] Then [| str2 # ? ++ %‘ :’ ++ ? → nb_prefixed_tags := nb_prefixed_tags + 1, str2 # %‘ xrce’ → nb_xrce_tags := nb_xrce_tags + 1, nb_other_tags := nb_other_tags + 1 |] Then (tags := tags + str2) ); print(‘number of opening tags :’ +str(nb_open_tags)) ; print(‘number of closing tags :’ +str(nb_close_tags)) ; print(‘number of empty tags :’ +str(nb_emp_tags)) ; print(‘number of prefixed tags :’ +str(nb_prefixed_tags)) ; print(‘number of xrce tags :’ +str(nb_xrce_tags)) ; print(‘number of other tags :’ +str(nb_other_tags)) ; print (tags)

In this example, an action system, i.e. a declarative statement, is composed with another action system and with an assignment instruction, i.e. an imperative statement, and the three are nested in a closure loop, i.e. an imperative statement.

To illustrate the compactness and readability provided by the proposed technique, the same program coded with only the closure loop and if-then-else statements is presented in the following. For simplicity purposes, it is assumed that boolean expressions in conditions can be replaced by pattern-matching expressions.

*( if (length(str1) > 0) then ( if (str1 # ? ++ %‘ <’ ++ ?str2 ++ %‘ >’ ++ ?str1) then ( if (str2 # ? ++ %‘ :’ ++ ?) then ( nb_open_tags := nb_open_tags + 1; nb_prefixed_tags := nb_prefixed_tags + 1 ) else ( if (str2 # %‘ xrce’ ++ ?) then ( nb_open_tags := nb_open_tags + 1; nb_xrce_tags := nb_xrce_tags + 1 ) else ( nb_open_tags := nb_open_tags + 1; nb_other_tags := nb_other_tags + 1 ) ); tags := tags + str2 ) else ( if (str1 # ? ++ %‘ </’ ++ ?str2 ++ %‘ >’ ++ ?str1) then ( if (str2 # ? ++ %‘ :’ ++ ?) then ( nb_close_tags := nb_close_tags + 1; nb_prefixed_tags := nb_prefixed_tags + 1 ) else ( if (str2 # %‘ xrce’ ++ ?) then ( nb_close_tags := nb_close tags + 1; nb_xrce_tags := nb_xrce_tags + 1 ) else ( nb_close_tags := nb_close tags + 1; nb_other_tags := nb_other tags + 1 ) ); tags := tags + str2 ) else ( if (? ++ %‘ <’ ++ ?str2 ++ %‘ />’ ++ ?str1) then ( if (str2 # ? ++ %‘ :’ ++ ?) then ( nb_emp_tags := nb_emp_tags + 1; nb_prefixed_tags := nb_prefixed_tags + 1 ) else ( if (str2 # %‘ xrce’ ++ ?) then ( nb_emp_tags := nb_emp_tags + 1; nb_xrce_tags := nb_xrce_tags + 1 ) else ( nb_emp_tags := nb_emp_tags + 1; nb_other_tags := nb_other_tags + 1 ) ); tags := tags + str2 ) ) ) ) else (none) ); print(‘number of opening_tags :’ +str(nb_open_tags)) ; print(‘number of closing_tags :’ +str(nb_close tags)) ; print(‘number of empty_tags :’ +str(nb_emp_tags)) ; print(‘number of prefixed_tags :’ +str(nb_prefixed_tags)) ; print(‘number of xrce_tags :’ +str(nb_xrce_tags)) ; print(‘number of other_tags :’ +str(nb_other_tags)) ; print (tags)

Using if-then-else statements, 3×3=9 cases have to be dealt with separately. In languages like C or Java, a Switch construct would have produced a more readable solution, but unfortunately, Switch can only be used with primary integer and char/byte types, and test values have to be statically defined.

It will be appreciated that the above described technique enables the definition of basic imperative and declarative statements, the combination of both kinds of operations in meaningful and arbitrarily complex control structures, and the typing of such constructions.

Main applications are language construction, refining control abstractions and programmation models toward more computational expressiveness, rewriting systems theory, classical conditional term rewriting systems, explicit strategies, transformation languages and systems for compilation and language processing, structured document transformation, tree pattern matching, event management, explicit or automatic document transformation systems, and others.

The technique provides material for building languages which are less abstract and more general than rewriting systems but abstract enough to simplify and extend general purpose programming languages. The technique could hence play an important role in the design of new transformation techniques or the extension of existing ones. Further, the technique may solve data and document interchange problems which will lead to low costs and high quality solutions.

Moreover, the technique provides a programming language framework through the definition of generic syntactic constructs, their operational semantics and the associated formal type system. The important difficulties which underlie the ambitious goal of bringing together declarative and imperative programming styles are solved all together, in a conceptually new and generic approach.

It will be appreciated that the bi-valuation technique enables and complements the symmetrical structural pattern matching technique that has been described above. The technique is further particular suited for supporting general typing mechanisms. Type checking improves the global reliability of programming languages, and provides efficient means for optimization, thus, increasing the runtime performance. This will be described in more detail below.

By means of bi-valuation of programming statements, and by using imperative connectors, the design of new programming languages or the extension of existing programming languages are rendered possible with constructions that provide innovative expressiveness. Resulting languages are located at an intermediate level of abstraction, between declarative, functional and imperative languages. This kind of abstraction could play an important role in general structure transformation technologies. When building modern programming languages with adequate control abstraction level, the major difficulty is to find the best trade-off between simplicity and power, conciseness and expressiveness. It will therefore be appreciated that the present technique provides programming constructs in such a way that the constructs offer compositional capabilities. Further, connectors are provided that allow the composition of such constructs with relevant semantics. Further, a sound type system is provided which enables to check out composition errors.

Statement Evaluation Syntax and Operational Semantics

In the following, the syntax and semantics of the basic conditional and imperative constructs are described in more detail.

In the testing operation, environment modifications are allowed during evaluation of the boolean part. This enables the use of complex operations with effects on context as a condition for the test:

$\begin{matrix} {\frac{{\Gamma \vdash e_{1}}->{\Gamma^{\prime} \vdash e_{1}^{\prime}}}{{\Gamma \vdash \;{{if}\mspace{20mu} e_{1}\mspace{25mu}{then}\mspace{20mu} e_{2}\mspace{14mu}{else}\mspace{14mu} e_{3}}}->{\Gamma^{\prime} \vdash {{if}\mspace{14mu} e_{1\mspace{20mu}}^{\prime}{then}\mspace{14mu} e_{2}\mspace{14mu}{else}\mspace{14mu} e_{3}}}}\mspace{14mu}\lbrack{if}\rbrack} & \; \end{matrix}$

-   -   if true then e₂ else e₃→e₂ [if1]     -   if false then e₂ else e₃ →e₃ [if2]

The sequencing operator is defined as shown hereafter:

$\begin{matrix} {\frac{{\Gamma \vdash e_{1}}->{\Gamma^{\prime} \vdash e_{1}^{\prime}}}{{\Gamma \vdash e_{1}};{e_{2}->{\Gamma^{\prime} \vdash e_{1}^{\prime}}};e_{2}}\mspace{14mu}\left\lbrack {{seq}\; 1} \right\rbrack} \\ {\frac{e_{1}->{\bullet\;\upsilon}}{e_{1};{e_{2}->e_{2}}}\mspace{14mu}\left\lbrack {{seq}2} \right\rbrack} \end{matrix}$

Further, an assignment operator is provided that finally reduces to a distinguished value “unit”. In the prior art, similar mechanisms for typing imperative statements exist for integrating imperative extensions in the functional framework. However, the result of such an assignment operation x:=e is just the result of the expression e itself:

$\begin{matrix} {\frac{{\Gamma \vdash e}->{\Gamma^{\prime} \vdash e^{\prime}}}{{\Gamma \vdash x}:={{e->{\Gamma^{\prime} \vdash x}}:=e^{\prime}}}\mspace{14mu}\left\lbrack {{assg}1} \right\rbrack} \\ {\frac{e->{\bullet{\;\mspace{11mu}}\upsilon}}{{{\Gamma,\mspace{11mu} x^{w}} \vdash x}:={e->{{\Gamma,\mspace{11mu} x^{\upsilon}} \vdash {unit}}}}\mspace{14mu}\left\lbrack {{assg}2} \right\rbrack} \end{matrix}$

Moreover, an iterative construct is provided in the form of a closure operator, noted *(e), which allows to iterate an action e until it evaluates to a distinguished value “none”. Thus, this action must be a declarative action, since applying the closure to a pure imperative action like an assignment would produce endless computation. This property is checked by the typing rules described below. The semantics of the * operator is simply defined by

-   -   *(e)→if (e!=none) then *(e) else unit [*]

A closure operation, returning always the distinguished value “unit”, is considered as a pure imperative (mono-valuated) statement.

Declarative constructs are defined as shown hereafter:

e ::= e

e′ rule [|e₁, . . . , e_(n)|] action system (ordered) {|e₁, . . . , e_(n)|} action system (unordered)

As mentioned above, the first universal construction is the rule, constituted of a left-hand condition and a right-hand action executed only if the condition can be verified. The proposed syntax, and the following type system, allows the cascading of rules of the form e₁

e₂

e₃, understood as e₁

(e₂

e₃):

-   -   e₁         e₂ → if (e₁==true) then e₂ else none [Rule]

In declarative environments, such rules are usually assembled in larger systems. The rule choice and application strategy varies a lot depending on the various proposed models, but most common tactics are “try rules in order” or “try and rule” and “iterate application as much as possible”. These standard models, and more sophisticated tactics as well, are easily emulated by combining the action systems with the closure operator presented above.

The ordered action system consists of a succession of actions to be tried in the given order. In the following definition, v_(f) denotes a “fresh” variable, i.e with a unique name:

$\begin{matrix} {{n \geq {2\mspace{14mu}\left\lbrack \left| {e_{1}{{,\ldots\;,}\;}e_{n}} \right| \right\rbrack}}->\left\lbrack {\begin{matrix} {{{var}\mspace{14mu}\upsilon_{f}} = {e_{1}.}} \\ {{{if}\left( {\upsilon_{f} == {none}} \right)}{\;\mspace{14mu}}} \\ {{then}\left\lbrack {\left| {e_{2},\mspace{11mu}\ldots} \right.\;,\;\left. e_{n} \right|} \right\rbrack} \\ {{else}\mspace{14mu}\upsilon_{f}} \end{matrix}\mspace{14mu}\left\lbrack {{Asys}l} \right\rbrack} \right.} \\ {\left\lbrack |e| \right\rbrack->{e\mspace{14mu}\left\lbrack {{Asys}2} \right\rbrack}} \end{matrix}$

It is to be noted that this definition implies that all actions e₁, . . . , e_(n−1) must be bi-valuated, i.e possibly evaluated to “none”; the last one can be mono-valuated. In that case the whole system becomes itself mono-valuated, since a value different from “none” will be finally returned.

Unordered action systems as similarly defined, up to the (random) choice of action e_(i) among all others:

$\begin{matrix} {{{n \geq {2,\; i}} \in {\left\{ {1,\ldots\;,n} \right\}\left\{ {\left| e_{1} \right.,\ldots\;,\left. e_{n} \right|} \right\}}}->\left\lbrack {\begin{matrix} {{{var}\mspace{20mu}\upsilon_{f}} = {e_{i},}} \\ {{if}\left( {\upsilon_{f} == {none}} \right)} \\ {{then}\left\{ {\left| e_{1} \right.,\ldots\;,\;{e_{i - 1},e_{i + 1}},\ldots\;,\;\left. e_{n} \right|} \right\}} \\ {{else}\mspace{14mu}\upsilon_{f}} \end{matrix}\mspace{14mu}\left\lbrack {{Asysb}1} \right\rbrack} \right.} \\ {\left\{ |e| \right\}->{e\mspace{14mu}\left\lbrack {{Asysb}2} \right\rbrack}} \end{matrix}$

One consequence of this definition is that here, all actions must be bi-valuated, in the sense given above. Such an action system specifies permutation equivalent rule collection, allowing great perspectives for run-time optimizations. Moreover, it allows the programmer to avoid producing over-specified code, making the code more understandable to external code reviewers or maintainers.

Turning now to the imperative connectors, the terminology “imperative connector” has been chosen by reference to the so-called “Boolean connectors”, in order to underline the similarity. In both cases, values are bi-valuated. But the present, more subtle approach to the valuation domains allows to handle several data sets.

The first useful connector is the well-known sequence “;” already presented.

Another useful set of operators are concurrent execution (binary) operators, noted e₁ ∥^ e₂ and e₁ ∥^(v) e₂, and also e₁ ∥* e₂ for both, equipped with the following interleaving semantics:

$\begin{matrix} {\frac{{\Gamma \vdash e_{1}}->{\Gamma^{\prime} \vdash e_{1}^{\prime}}}{\Gamma \vdash {e_{1}{^{*}{e_{2}->{\Gamma^{\prime} \vdash e_{1}^{\prime}}}}^{*}e_{2}}}\mspace{14mu}\lbrack{ParL}\rbrack} \\ {\frac{{\Gamma \vdash e_{2}}->{\Gamma^{\prime} \vdash e_{2}^{\prime}}}{\Gamma \vdash {e_{1}{^{*}{e_{2}->{\Gamma^{\prime} \vdash e_{1}}}}^{*}e_{2}^{\prime}}}\mspace{14mu}\lbrack{ParR}\rbrack} \end{matrix}$

The first operator ∥^, besides its concurrent semantics, behaves like a logical “and”, commonly noted ^, with respect to the management of the bi-valuation:

-   -   unit ∥^e₂→e₂ [Par1a] none ∥^e₂→e₂; none [Par1b]     -   e₁ ∥^none→e₁; none [Par1c] e₁ ∥^unit→e₁ [Par1d]         With ∥^(v), the equivalent “or” behavior, commonly noted v, is         expressed:     -   unit ∥^(v)e₂→e₂; unit [Par2a] none ∥^(v)e₂→e₂ [Par2b]     -   e₁ ∥^(v)none→e₁ [Par2c] e₁ ∥^(v)unit→e₁; unit [Par2d]

This definition describes a simple concurrent semantics, that can be extended to a wider scope. The important point here is to define this concurrent composition as a bi-valuated statement consistent with the global framework.

Besides concurrent imperative connectors, there are also provided sequential imperative connectors, Then, Else, And and Or.

Then and Else are defined by:

-   -   e₁ Then e₂ → if (e₁!=none) then e₂ else none [Then]     -   e₁ Else e₂ → var v_(f)=e₁. if (v_(f)==none) then e₂ else v_(f)         [Else]         v_(f) is a fresh variable, i.e. doesn't occur in the current         context nor in e₁ or e₂. The following And and Or operators are         quite similar, except that they impose the evaluation of both         operands whatever result is computed. Another difference is that         values computed by both operands can only range over {unit,         none}. In that sense, e₁ and e₂ are imperative expressions:     -   e₁ And e₂ → if (e₁!=none) then e₂ else (e₂; none) [And]     -   e₁ Or e₂ → if (e₁==none) then e₂ else (e₂; unit) [Or]         Type System

A minimal, formal, type system that illustrates and enables the use of the constructs is described hereafter. This type system is based on a subtyping relation using inclusion polymorphism brought by a non-discriminating union type constructor. An example of a type hierarchy is shown in FIG. 5.

The type system introduces other relevant tools, when required, to known techniques for building type systems. It is described through “typing judgments” logically organized by means of “typing equations”.

The typing judgement γ

e : t states that expression e has type t in the typing context γ. The notion of γ

t

t′ asserts that type t is a sub-type of type t′ and a more original judgement γ

e :: t, says that the expression e has minimal type t. Formally, this last relation is characterized by the following property:

-   -   (Minimal type) γ         e :: t iff∀t′, γ         e : t′         γ         t         t′

This notion of minimal type is useful in order to avoid overgeneralized inferences due to the so called subsumption rule (see [Sub]), while preserving the convenience of subtyping mechanism. The notation γ, x : t

. . . expresses that the fact that “x is a variable of type t” is registered in the typing context. More precisely, γ, x : t is a typing context γ′ such that γ′=γ∪{x:t}. The following rules define the reflexive and transitive subtyping relation

, for all well-formed types t, t′, t₁, t₂, t₃ and the typing context γ.

$\begin{matrix} {\gamma \vartriangleright t \preccurlyeq {t\lbrack{Refl}\rbrack}} \\ {\frac{\gamma \vartriangleright t_{1} \preccurlyeq {t_{2}\mspace{14mu}\gamma} \vartriangleright t_{2} \preccurlyeq t_{3}}{\gamma \vartriangleright t_{1} \preccurlyeq t_{3}}\lbrack{Trans}\rbrack} \\ {\frac{\gamma \vartriangleright e :: t}{{\gamma \vartriangleright e} :: t}\lbrack{Min}\rbrack} \\ {\frac{{\gamma \vartriangleright e} :: {{t\mspace{14mu}\gamma} \vartriangleright t \preccurlyeq t^{\prime}}}{{\gamma \vartriangleright e} :: t^{\prime}}\lbrack{Sub}\rbrack} \\ {\frac{\gamma \vartriangleright t_{1} \preccurlyeq {t\mspace{14mu}\gamma} \vartriangleright t_{2} \preccurlyeq t}{\gamma \vartriangleright {t_{1}{{t_{2} \preccurlyeq t}}}}\left\lbrack {U1} \right\rbrack} \\ {\frac{\gamma \vartriangleright t \preccurlyeq t_{1}}{\gamma \vartriangleright t \preccurlyeq {t_{1}{t_{2}}}}\left\lbrack {U2} \right\rbrack} \\ {\frac{\gamma \vartriangleright t \preccurlyeq t_{2}}{\gamma \vartriangleright t \preccurlyeq {t_{1}{t_{2}}}}\left\lbrack {U3} \right\rbrack} \end{matrix}$

Rules for defining well-formedness conditions on types will not be shown, and types are supposed well formed in all following definitions. An important type constructor is the non-discriminating union which provides inclusion polymorphism.

Basic typing of literals and variables is defined by the following axioms:

-   -   ∅         n :: int [Num] ∅         s :: string [Str] γ,x:t         x :: t [Var]         -   ∅             true :: bool [Bool1] ∅             false :: bool [Bool2]         -   ∅             none :: None [None] ∅             unit :: Unit [Unit]

Operators are typed by:

$\begin{matrix} {{*{\in \left\{ {{{+ ,}\;*,}\; - {,\;/}} \right\}}}\mspace{25mu}} \\ {\frac{{\gamma \vartriangleright e_{1}} :: {{{int}\mspace{20mu}\gamma} \vartriangleright e_{2}} :: {int}}{\gamma \vartriangleright {e_{1}*e_{2}} :: {int}}\left\lbrack {{Add}\; 1} \right\rbrack} \\ {\frac{{\gamma \vartriangleright e_{1}} :: {{{string}\mspace{14mu}\gamma} \vartriangleright e_{2}} :: {string}}{\gamma \vartriangleright {e_{1} + e_{2}} :: {string}}\left\lbrack {{Add}2} \right\rbrack} \\ {\frac{{\gamma \vartriangleright e_{1}} :: {{t\mspace{14mu}\gamma} \vartriangleright e_{2}} :: t^{\prime}}{\gamma \vartriangleright e_{1}==e_{2} :: {bool}}\lbrack{Eq}\rbrack} \\ {\frac{{\gamma \vartriangleright e_{1}} :: {{t\mspace{14mu}\gamma} \vartriangleright e_{2}} :: t^{\prime}}{\gamma \vartriangleright e_{1}!=e_{2} :: {bool}}\lbrack{Neq}\rbrack} \\ {\frac{{\gamma \vartriangleright e} :: {{{bool}\mspace{20mu}\gamma} \vartriangleright e_{1} :: {t_{1}\mspace{11mu}\gamma} \vartriangleright e_{2} :: t_{2}}}{\gamma \vartriangleright {{if}\mspace{14mu} e\mspace{14mu}{then}\mspace{14mu} e_{1}\mspace{14mu}{else}\mspace{14mu} e_{2}} :: {t_{1}{t_{2}\mspace{11mu}}}}\lbrack{If}\rbrack} \\ {\frac{{x \notin {{{{dom}(\gamma)}\mspace{14mu}\gamma} \vartriangleright e_{1}}} :: {t\mspace{14mu}\gamma,\;{e_{1}:{t \vartriangleright e_{2} :: t_{2}}}}}{{\gamma \vartriangleright {{var}\mspace{11mu} x}} = {{e_{1} \cdot e_{2}} :: t_{2}}}\;\lbrack{Var}\rbrack} \end{matrix}$ It is to be noted that [Eq,Neq] do not impose operands to have the same common type. That is, equality comparisons are applicable to every kind of data.

Other basic constructs can be typed through:

$\begin{matrix} {\frac{{\gamma \vartriangleright e_{1}} :: {{Unit}{{{{None}\mspace{14mu}\gamma} \vartriangleright e_{2} :: t_{2}}}}}{{\gamma \vartriangleright e_{1}};{e_{2} :: t_{2}}}\lbrack{Seq}\rbrack} \\ {\frac{{\gamma \vartriangleright x :: {t\mspace{14mu}\gamma} \vartriangleright e} :: t}{{{\gamma \vartriangleright x} ::} = {e :: {Unit}}}\lbrack{Assg}\rbrack} \\ {\frac{\gamma \vartriangleright e :: {{Unit}{{None}}}}{\gamma \vartriangleright {*(e)} :: {Unit}}\lbrack{Clos}\rbrack} \end{matrix}$

Concurrent connectors are always bi-valuated imperatives:

$\begin{matrix} {{\frac{\gamma \vartriangleright e_{1} :: {{Unit}{{{{{None}\mspace{14mu}\gamma} \vartriangleright e_{2}} :: {{Unit}{{None}}}}}}}{\gamma \vartriangleright {e_{1}{^{\hat{}}{e_{2} :: {{Unit}{{None}}}}}}}\;\lbrack{ParAnd}\rbrack}\mspace{31mu}} \\ {\frac{\gamma \vartriangleright e_{1} :: {{Unit}{{{{None}\mspace{11mu}\gamma} \vartriangleright e_{2} :: {{Unit}{{None}}}}}}}{\gamma \vartriangleright {e_{1}{^{\bigvee}{e_{2} :: {{Unit}{{None}}}}}}}\lbrack{ParOr}\rbrack} \end{matrix}$

The following equations achieve very precise type control for “and-like” concurrence:

$\begin{matrix} {{\frac{{\gamma \vartriangleright e_{1}} :: {{{Unit}\mspace{20mu}\gamma} \vartriangleright e_{2}} :: {Unit}}{\gamma \vartriangleright {e_{1}{^{\hat{}}{e_{2} :: {Unit}}}}}\;\left\lbrack {{ParAnd}\; 1} \right\rbrack}\mspace{20mu}} \\ {\frac{{\gamma \vartriangleright e_{1}} :: {{{None}\mspace{14mu}\gamma} \vartriangleright e_{2}} :: {{Unit}{{None}}}}{\gamma \vartriangleright {e_{1}{^{\hat{}}{e_{2} :: {None}}}}}\left\lbrack {{ParAnd}\; 2} \right\rbrack} \end{matrix}$

Similarly, for “or-like” concurrence:

$\begin{matrix} {{\frac{{\gamma \vartriangleright e_{1}} :: {{{None}\mspace{14mu}\gamma} \vartriangleright e_{2} :: {None}}}{\gamma \vartriangleright {e_{1}{^{\bigvee}{e_{2} :: {None}}}}}\;\left\lbrack {{ParOr}\; 1} \right\rbrack}\mspace{45mu}} \\ {\frac{{\gamma \vartriangleright e_{1}} :: {{{Unit}\mspace{14mu}\gamma} \vartriangleright e_{2}} :: {{Unit}{{None}}}}{\gamma \vartriangleright {e_{1}{^{\bigvee}{e_{2} :: {Unit}}}}}\left\lbrack {{ParOr}\; 2} \right\rbrack} \\ {\frac{{\gamma \vartriangleright e_{1}} :: {{None}{{{{{Unit}\mspace{20mu}\gamma} \vartriangleright e_{2}} :: {Unit}}}}}{\gamma \vartriangleright {e_{1}{^{\bigvee}{e_{2} :: {Unit}}}}}\left\lbrack {{ParOr}3} \right\rbrack} \end{matrix}$

Sequential connectors are defined by, for some t, t′:

$\begin{matrix} {{\frac{\gamma \vartriangleright e_{1} :: {{Unit}{{{{None}\mspace{11mu}\gamma} \vartriangleright e_{2} :: t}}}}{\gamma \vartriangleright {e_{1}\mspace{14mu}{Then}\mspace{14mu} e_{2}} :: {t{{None}}}}\;\lbrack{Then}\rbrack}\mspace{25mu}} \\ {\frac{\gamma \vartriangleright e_{1} :: {t{{{{None}\mspace{14mu}\gamma} \vartriangleright e_{2} :: t^{\prime}}}}}{\gamma \vartriangleright {e_{1\mspace{14mu}}{Else}\mspace{20mu} e_{2}} :: t \mid t^{\prime}}\lbrack{Else}\rbrack} \\ {\frac{\gamma \vartriangleright e_{1} :: {{Unit}{{{{None}\mspace{25mu}\gamma} \vartriangleright e_{2} :: {{Unit}{{None}}}}}}}{\gamma \vartriangleright {e_{1}\mspace{14mu}{And}\mspace{14mu} e_{2}} :: {{Unit}{{None}}}}\;\lbrack{And}\rbrack} \\ {\frac{\gamma \vartriangleright e_{1} :: {{Unit}{{{{None}\mspace{14mu}\gamma} \vartriangleright e_{2} :: {{Unit}{{None}}}}}}}{\gamma \vartriangleright {e_{1}\mspace{20mu}{Or}\mspace{14mu} e_{2}} :: {{Unit}{{None}}}}\lbrack{Or}\rbrack} \end{matrix}$

Beyond these last two equations, as for concurrent connectors, much more precise type checking is provided in order to detect composition errors:

$\begin{matrix} {{\frac{{\gamma \vartriangleright e_{1} :: {{None}\mspace{14mu}\gamma} \vartriangleright e_{2}} :: {{Unit}{{None}}}}{\gamma \vartriangleright {e_{1}\mspace{14mu}{And}\mspace{14mu} e_{2}} :: {None}}\left\lbrack {{And}1} \right\rbrack}\mspace{95mu}} \\ {\frac{{\gamma \vartriangleright e_{1}} :: {{Unit}{{{{None}\mspace{14mu}\gamma} \vartriangleright e_{2} :: {None}}}}}{\gamma \vartriangleright {e_{1}\mspace{14mu}{And}\mspace{14mu} e_{2}} :: {None}}\left\lbrack {{And}2} \right\rbrack} \\ {\frac{{\gamma \vartriangleright e_{1} :: {{Unit}\mspace{14mu}\gamma} \vartriangleright e_{2} :: {Unit}}\mspace{14mu}}{\gamma \vartriangleright {e_{1}\mspace{14mu}{And}\mspace{14mu} e_{2}} :: {Unit}}\left\lbrack {{And}3} \right\rbrack} \\ {\frac{{\gamma \vartriangleright e_{1} :: {{Unit}\mspace{14mu}\gamma} \vartriangleright e_{2}} :: {{Unit}{{None}}}}{{\gamma \vartriangleright {e_{1}\mspace{20mu}{Or}\mspace{14mu} e_{2}} :: {Unit}}\mspace{11mu}}\;\left\lbrack {{Or}\; 1} \right\rbrack} \\ {\frac{\gamma \vartriangleright e_{1} :: {{Unit}{{{{None}\mspace{14mu}\gamma} \vartriangleright e_{2} :: {Unit}}}}}{\gamma \vartriangleright {e_{1}\mspace{14mu}{Or}\mspace{14mu} e_{2}} :: {Unit}}\left\lbrack {{Or}2} \right\rbrack} \\ {\frac{\gamma \vartriangleright e_{1} :: {{None}\mspace{14mu}\gamma} \vartriangleright e_{2} :: {None}}{\gamma \vartriangleright {e_{1}\mspace{14mu}{Or}\mspace{14mu} e_{2}} :: {None}}\left\lbrack {{Or}\; 3} \right\rbrack} \end{matrix}$

The relevance of the typing approach described above with respect to the semantics of the language constructs described above is now demonstrated in more detail.

The soundness property is to prove that a well typed construct cannot produce an error during evaluation. More precisely, it establishes the conservation of the well typed property: any well typed expression remains well typed over all possible computation steps. One preliminary step is to define a logical relation between the typing context and the execution environment:

The execution environment Γ is conform with respect to the typing context γ (noted γ

Γ):

-   -   γ         Γ iff ∀x^(v) ∈Γ, γ         x :: t ^ ∅         v :: t

In the following, e is understood as a non-reduced syntactic expression. The type is preserved during reduction:

${{\forall{{t,\mspace{11mu}\gamma,\mspace{11mu}\Gamma\mspace{31mu}\gamma} \mid}} \vdash {{\Gamma\mspace{25mu}{and}\mspace{14mu}\gamma} \vartriangleright e}} :: \left. t\Rightarrow\left\{ \begin{matrix} {\Gamma \vdash \left. e\rightarrow\Gamma^{\prime} \right. \vdash e^{\prime}} \\ {{\gamma \vartriangleright e^{\prime}} :: t} \\ {{\gamma \mid} \vdash \Gamma^{\prime}} \end{matrix} \right. \right.$ The proof presents no particular difficulty; it uses an induction on the structure of e.

The completeness property states that all derivations, i.e. computation steps, defined by the operational semantics is covered by the type control mechanism. Completeness is defined by:

${\forall e},\gamma,\left. {\Gamma\;{s.t.\gamma}} \middle| {\vdash {\Gamma\mspace{14mu}\Gamma} \vdash \left. e\rightarrow\Gamma^{\prime} \right. \vdash \left. e^{\prime}\Longrightarrow\mspace{14mu}{\exists\;{t\mspace{14mu}{such}\mspace{14mu}{that}\mspace{14mu}\left\{ \begin{matrix} {\gamma \vartriangleright {e:t}} \\ {\gamma \vartriangleright {e^{\prime}:t}} \\ \left. \gamma \middle| {\vdash \Gamma^{\prime}} \right. \end{matrix} \right.}} \right.} \right.$ As for the preservation of type, the proof presents no particular difficulty; it uses an induction on the structure of e.

Using the foregoing specification, the invention may be implemented as a machine (or system), process (or method), or article of manufacture by using standard programming and/or engineering techniques to produce programming software, firmware, hardware, or any combination thereof.

Any resulting program(s), having computer-readable program code, may be embodied within one or more computer-usable media such as memory devices or transmitting devices, thereby making a computer program product or article of manufacture according to the invention. As such, the terms “article of manufacture” and “computer program product” as used herein are intended to encompass a computer program existent (permanently, temporarily, or transitorily) on any computer-usable medium such as on any memory device or in any transmitting device.

The invention has been described with reference to particular embodiments. Modifications and alterations will occur to others upon reading and understanding this specification taken together with the drawings. The embodiments are but examples, and various alternatives, modifications, variations or improvements may be made by those skilled in the art from this teaching which are intended to be encompassed by the following claims. 

1. A program stored on a computer-readable medium that causes a computer to execute steps to determine whether a computer-storable expression matches a filter, comprising: identifying and testing the structural form of the expression; choosing a transformation model for the filter that is compatible with the structural form of the expression; evaluating a first code structure representing the expression to determine a value of said expression prior to filtering; analyzing a second code structure representing the filter to determine the characteristics of the filter, wherein the second code structure comprises a plurality of filter characteristics corresponding to a plurality of structural forms of the first code structure, the plurality of structural forms comprising text, trees, and graphs, and wherein the second code structure applies a filter corresponding to the structural form of the first code structure; and filtering said evaluated value according to the filter characteristics, wherein said first code structure is constructed from a plurality of first programming language code structure elements and said second code structure is constructed from a plurality of second programming language code structure elements, each second structure element being symmetrically constructed to correspond to one of said first structure elements, and wherein evaluating, analyzing and filtering are performed upon explicit invocation of a matching operator, and filtering comprises returning a boolean evaluation result value.
 2. The computer-readable medium of claim 1, wherein the first code structure and the second code structure are part of a compiler programming language code.
 3. The computer-readable medium of claim 1, wherein the second code structure includes at least one composition operator acting as a logical connector for logically combining two of said programming language code structure elements, or for inverting the boolean value of at least one of said second programming language code structure elements.
 4. The computer-readable medium of claim 1, wherein the first code structure includes a first concatenation operator for concatenating two expressions, and the second code structure includes a second concatenation operator for concatenating two filter elements, the first and the second concatenation operators being applied within the first code structure and the second code structure, respectively, in essentially the same manner.
 5. The computer-readable medium of claim 1, wherein the first and the second code structures include indicator elements indicating a data type, the indicator elements acting as structure constructors in the first code structure and as filter constructors in the second code structure, each of the structure constructors corresponding to a respective one of the filter constructors.
 6. The computer-readable medium of claim 1, wherein the second code structure includes a test operator having an operand, and wherein filtering comprises testing the occurrence of the value of said operand in the expression.
 7. The computer-readable medium of claim 1, wherein the second code structure includes an existence operator that matching any element that exists.
 8. The computer-readable medium of claim 1, wherein the second code structure includes an assignment operator having an operand, to assign a part of the expression to a variable that is identified by said operand.
 9. The computer-readable medium of claim 1, wherein the second code structure includes a Kleene operator.
 10. The computer-readable medium of claim 1, wherein the second code structure includes a do operator having two arguments, one argument being a filter and the other argument being an instruction or a sequence of instructions, wherein filtering includes executing the instructions only if the filter is successful.
 11. The computer-readable medium of claim 1, wherein the filter is a recursive filter enabling filtering of trees.
 12. The computer-readable medium of claim 1, wherein the filter is a normalized filter.
 13. The computer-readable medium of claim 1, wherein filtering includes modifying the environment of the computer system, the environment including variables and corresponding values used by the computer system when filtering said value of said expression.
 14. The computer-readable medium of claim 1, wherein the first code structure and the second code structure are part of an interpreter programming language code. 